Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bundle exec not working with crontab

I'm 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

The crontab entry is

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

I'm getting 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 dont get the error when I run the commands manually. Not sure what's going on here. Could someone please point out.

Thanks

like image 994
nish Avatar asked Nov 07 '13 09:11

nish


2 Answers

A nice trick to get all environment properly set up 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 a full login shell, thus reading your bashrc file and any path / rvm setting it performs.

If you want to simplify your crontab management and use this trick - as well as others - without having to think about them, you can use the Whenever gem. It also play very nice with capistrano, if you use it, regenerating crontab on deploy.

like image 95
kik Avatar answered Sep 27 '22 21:09

kik


The user used by cron does not have the correct environment. You can tell cron which user to use. For a bash script, you can so something like:

#!/bin/bash --login
source /home/user/.bashrc
rvm use 2.0.0@gemset #if you use rvm
cd /path/to/project && bundle exec xyz
like image 35
Bjoernsen Avatar answered Sep 27 '22 23:09

Bjoernsen