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

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

Related

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

why bash -l -c "CMD" makes ruby find my gem?

In my ruby script,I required the gmail gem:
require 'rubygems'
require 'gmail'
when running in shell,it works ok:
ruby my-script.rb
while when I put it in a cron job,it failed to execuate:
* * * * * cd /to/script/directory;/usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/ruby ./my-script.rb
the log shows that the gmail gem can not be loaded:
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- gmail (LoadError)
well, when i do this(put the cmd in bash -l -c 'CMD'):
* * * * * /bin/bash -l -c 'cd /to/script/directory;/usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/ruby ./my-script.rb'
it works ok again.
why?
ps.I know the arg -l make the bash a login shell,but does that make any difference?
The -l parameter executes the command in a login shell, which means that it inherits your path and other settings from your shell profile. The cron job, if run without the login shell, will be run without any path environment variables set (such as those set by RVM), which results in the system being unable to find the referenced gems.
rvm requires you use a shell login, see FAQ.
I suspect you are loading RVM in your login script, ergo RVM will only be available in a login shell.

Can the whenever gem preserve existing lines in a crontab file?

I am using:
Ruby 1.9.2
whenever 0.7.2
capistrano 2.9.0
capistrano-ext 1.2.1
I am using whenever in conjunction with Capistrano on deploys to manage my crontab files.
I noticed that it completely rewrites my crontab files each time.
I'd like to be able to set environment variables in cron to control PATH and MAILTO settings, which are regular cron environment variables.
Is there a way to make whenever not overwrite the entire crontab file, so that I can add customizations to my crontab file and be sure that they will persist?
Yes, you can do this. You'll just need to assign an identifier to the task being written to crontab:
whenever --update-crontab some_identifier_name
It will generate an entry in crontab like this:
# Begin Whenever generated tasks for: some_identifier_name
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /var/www/test/releases/20120416183153 && script/rails runner -e production '\''Model.some_method'\'' >> /tmp/cron_log.log 2>&1'
# End Whenever generated tasks for: some_identifier_name
Then whenever you call the command above it will only update where it finds the identifier you specified.

Crontab rake job is not working with whenever gem in 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

Resources