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!

5
  • 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 Commented Oct 22, 2011 at 9: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. Commented Oct 22, 2011 at 9:38
  • google.com/search?&q=site%3Astackoverflow.com+rake+crontab Commented Oct 22, 2011 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? Commented Oct 22, 2011 at 18:38
  • Well, the very first on I get is this: stackoverflow.com/questions/285717/…, 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. Commented Oct 22, 2011 at 23:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 it might be better to post this as a new question with a bit more context.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.