The exec package does not work with crontab

I am trying to execute the following shell script using crontab:

#!/bin/sh cd /mnt/voylla-production/current bundle exec rake maintenance:last_2_days_orders bundle exec rake maintenance:send_last_2_days_payment_dropouts 

Crontab entry

 0 16 * * * /mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh 

I get the following error message in the mail:

 /mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 3: bundle: command not found /mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 4: bundle: command not found 

I do not get an error when running commands manually. Not sure what is going on here. Maybe someone will indicate.

thanks

+8
ruby-on-rails crontab bundle rake
source share
2 answers

A good trick to configure the entire environment installed in crontab is to use /bin/bash -l :

 0 16 * * * /bin/bash -l -c '/mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh' 

The -l option will invoke the full login shell, thus reading your bashrc file and any settings for the / rvm path that it executes.

If you want to simplify crontab management and use this trick - like the others - without having to think about them, you can use Whenever gem . It also plays great with capistrano if you use it, regenerating crontab during deployment.

+26
source share

A user using cron does not have the correct environment. You can tell cron which user to use. For a bash script, you can do this:

 #!/bin/bash --login source /home/user/.bashrc rvm use 2.0.0@gemset #if you use rvm cd /path/to/project && bundle exec xyz 
+6
source share

All Articles