1

I am trying to run a RAKE file from a bash script fired by a crontab:

my crontab looks like this:

* * * * * /bin/bash ~/sites/www/tweeet/get_tweeet.sh

my bash script (get_tweeet.sh) looks like this:

  1 #!/bin/bash
  2 set -x
  3 cd /var/www/tweeet/
  4 export RAILS_ENV=development
  5 rake get_tweeet >> /var/www/tweeet/test.log
  6 echo "$(date): cron job run    " >> /var/www/tweeet/test.log

What happens is that line 6 outputs into the test.log but line 5 does now - the rake does not run.

BUT if i call the script using the exact line from the crontab

/bin/bash /var/www/tweeet/get_tweeet.sh

then it works - i'm baffled by this!

toast
  • 582
  • 1
  • 6
  • 20
  • Is `rake` in the `PATH` of your `crond(8)`? If not, try adding the directory to your `/etc/environment` file if your system supports it (and your PAM is configured to use it) or directly to your `crontab(5)` file with `PATH=/sbin:/usr/sbin:/bin:/usr/bin:/path/to/rake/home` – sarnold Oct 22 '11 at 09:28
  • do you log the output (especially stderr) from your cron job somewhere? That might give you some more clarity as to what's actually happening. – Frost Oct 22 '11 at 09:38
  • http://www.google.com/search?&q=site%3Astackoverflow.com+rake+crontab – Thilo Oct 22 '11 at 14:22
  • cheers thilo, thats pretty helpful - y'know searching stackoverflow never crossed my mind. Really. Now do you have any particular link that you wish to highlight as from what i can see each of those links relate to different issues, from which user is running the cron to using which rake to find the full path and numerous other things which i have tried. So do you have anything to add or is troling SO your idea of a fun saturday night? – toast Oct 22 '11 at 18:38
  • Well, the very first on I get is this: http://stackoverflow.com/questions/285717/a-cron-job-for-rails-best-practices, which is pretty comprehensive. Your basic problem is that cron doesn't source the user's environment settings, so you either have to provide that in the crontab itself, or use a login shell. But it's really been beaten to death on this site already. If you think that's trolling, good luck to you. – Thilo Oct 22 '11 at 23:02

1 Answers1

1

Your direct execution of the script runs because it has your full environment available. A cronjob does not, even if it belongs to the same user. So one solution is to launch it from within a full login shell. In your crontab:

bash --login -c '/var/www/tweeet/get_tweeet.sh'

I remember seeing some post about this potentially having some subtle side effects that I don't recall, but it's been working for me.

Thilo
  • 17,565
  • 5
  • 68
  • 84
  • I have crontab tasks which run `* * * * * /bin/bash -l -c 'cd /home/ubuntu/workspace/rails/Feb20/my_app && RAILS_ENV=development bundle exec rake schedule:monthly --silent >> log/whenever.log 2>&1` but my BASH environments are not being used at all - I get blanks when I print out the bash variables (in bashrc file). And I am already using full login - what am I missing here? pls help – Biju Feb 22 '19 at 05:20
  • @Biju it might be better to post this as a new question with a bit more context. – Thilo Feb 23 '19 at 12:06