Crontab rake job is not working with whenever gem in ruby - ruby

This is my crontab file generated by whenever:
* * * * * /bin/bash -l -c 'cd /var/apps/path && RAILS_ENV=production echo "Testing" >> /new
* * * * * /bin/bash -l -c 'cd /var/apps/path && RAILS_ENV=production rake somegroup:rake_job --silent'
The first line is for test only and works as expected. However the rake job in the other line has nothing to do at all. In /var/log/syslog, both jobs are executed. Also tried using the absolute path of rake (/usr/local/rvm/gems/ruby-1.9.2-p136/bin/rake), still no luck. Don't know where to find error message. I'm using ubuntu 10.04TS server.
Any thoughts will be appreciated! ;)
J

are you using rvm and gemsets ?
I think I've found a problem with Whenever and rvm's rvmrc file

Related

Cron is running in home directory instead of file directory

I followed some other posts in stackoverflow and successfully setup cron with RVM using rvm cron setup and injected some ENV to the crontab file.
When I was troubleshooting why the dotenv gem is not working, I realised the following:
I placed my test.rb in file path /home/myuser/ruby/test.rb and had my crontab file as shown below:
* * * * * ruby /home/myuser/ruby/test.rb >> /home/myuser/ruby/output.log
and when I puts the output of the test.rb with Dir.pwd. The output states that the rb is run in the /home/myuser/ directory instead of /home/myuser/ruby directory.
While I had a hotfix by manually changing the path. But I wonder why it is the case.
By default, cron tasks of a user are executed from the user's home directory. In order to execute the script from proper directory, you have to "cd" to it.
Consider changing your crontab to:
* * * * * cd /home/myuser/ruby && ruby ./test.rb >> /home/myuser/ruby/output.log
Good luck!
According to #Pawel Dawczak who left the answer in the comment.
the solution is to rewrite the statement in crontab as
* * * * * cd /home/myuser/ruby && ruby test.rb >> /home/myuser/ruby/output.log
Thanks!

Ruby Bundle not running via cronjob

I'm trying to run a ruby bundle from a cronjob and it's not working, yet if I run the bundle from bash command-line, it works fine. Am I missing something?
The command is:
BUNDLE_GEMFILE=/client/Gemfile bundle exec /client/bin/runner
I've set it in crontab as follows:
*/1 * * * * BUNDLE_GEMFILE=/client/Gemfile bundle exec /client/bin/runner
But am getting no results. I really appreciate the help all!
Thanks,
Justin
Little did I know rbenv was being used, so the fix is:
*/1 * * * * bash -lc 'BUNDLE_GEMFILE=/client/Gemfile bundle exec /client/bin/runner'

Cronjob does not work on rake command

I am totally stuck on a cronjob command which does not want to work.
Here is my problem :
I just want to launch a rake command on my crontab (for Redmine : check all unread emails on a specific address through IMAP and create a redmine ticket)
Here is the command :
cd /opt/redmine/ && rake redmine:email:receive_imap RAILS_ENV="production" host=imap.gmail.com username=redmine.check#gmail.com ssl=true port=993 password=MyPassword project=level1support unknown_user=accept no_permission_check=1 allow_override=project
I am able to launch this on my command line and everything works fine
So I made a crontab -e and add this line on my crontab :
*/10 * * * * cd /opt/redmine/ && rake redmine:email:receive_imap RAILS_ENV="production" host=imap.gmail.com username=redmine.check#gmail.com ssl=true port=993 password=MyPassword project=level1support unknown_user=accept no_permission_check=1 allow_override=project
But it does not work.
The cron seems to run (it is OK in my /var/log/cron file)
I added a log file on this cron like this (at the end of the line) > /tmp/crontabRedmin.log 2&>1 but nothing is written on the log file.
I created a .sh script, I tried different syntaxes for the rake command (/usr/local/bin/rake or && RAILS_ENV=production bundle exec rake), everything works on my command line , but nothing through the cron.
Please help me to know, what am I missing.
When the rake task is executed using the cron job the .bash_profile file is not processed so you need to add the environment variables to the sell script.
Find the Ruby Gems path etc using the following:
echo $GEM_HOME
echo $GEM_PATH
echo $PATH
Add the following to your shell script:
#!/bin/bash
export PATH=$PATH:/home/user/.rvm/gems/ruby-2.0.0-p0/bin:/home/user/.rvm/gems/ruby-2.0.0-p0#global/bin:/home/user/.rvm/bin
export GEM_HOME=/home/user/.rvm/gems/ruby-2.0.0-p0
export GEM_PATH=/home/user/.rvm/gems/ruby-2.0.0-p0:/home/user/.rvm/gems/ruby-2.0.0-p0#global

Rake command not working in cron but works when running as command

I have placed the following in crontab (Executes for every minute)
* * * * * cd /home/foo/Projects/redmine-2-4-2-prod && /home/foo/.rvm/gems/ruby-2.0.0-p0/bin/rake RAILS_ENV=production --silent redmine:email:receive_imap host=imap.gmail.com port=993 ssl=1 username=foo#gmail.com password='foopass' project=testredmine tracker=Support status=New priority=High allow_override=project,status,tracker,priority
I see cron running every minute in logs (/var/log/syslog). On executing the above command in shell, I receive e-mails. But not receiving e-mails when the same command is executed in cron.
Please help me to know, what am I missing.
Try to add somethin like this to your cron task:
source /home/user/.rvm/environments/ruby-1.9.3
Path to your rvm files you can get by:
rvm env --path

Can't get rails rake task to play nice with crontab

I currently have this shell script ...
nightly.sh
#!/bin/bash
rvm 1.9.2
cd /home/appname/capistrano/current
RAILS_ENV=production bundle exec rake nightly >> /home/appname/capistrano/shared/log/nightly.log 2>&1
I use it in my crontab entry here... crontab -e
42 20 * * * /home/appname/nightly.sh
When it runs I get this error
/home/appname/nightly.sh: line 4: bundle: command not found
I am using RVM
I've now Added some environmental variables to my crontab per #KL-7
SHELL=/bin/bash
HOME=/home/appname
PATH=/home/appname/local/bin:/home/appname/.rvm/gems/ruby-1.9.2-p290/bin:/home/appname/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/appname/.rvm/rubies/ruby-1.9.2-p290/bin:/home/appname/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Now I'm getting this...
/home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find bundler
[minitest-1.6.0, rake-0.8.7, rdoc-2.5.8] (Gem::LoadError)
from /home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
from /home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems.rb:1210:in `gem'
from /home/appname/.rvm/gems/ruby-1.9.2-p290/bin/bundle:18:in `<main>'
It could be because its throwing an error and you are not capturing it. Try the following:
01 04 * * * /bin/bash -l -c 'cd /home/appname/capistrano/current && RAILS_ENV=production bundle exec rake nightly' >> /home/appname/capistrano/shared/log/nightly.log 2>&1
Seems like cron can't locate your bundle executable. You need to find it (using, for example, which bundle) and then either specify full path to it in crontab or set PATH environment variable at the top of crontab like that:
PATH=/bin:/usr/bin:/path/to/directory/with/bundle/
This might help:
/bin/bash -l -c
Read:
http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
Here is another solution:
* * * * * ssh localhost 'your command here...'
This will just ssh in to the same host (which sources the normal environment) and issues the command

Resources