I've a bash script that runs a ruby script that fetches my twitter feeds.
## /home/username/twittercron
#!/bin/bash
cd /home/username/twitter
ruby twitter.rb friends
It runs successfully in command line.
/home/username/twittercron
But when I try to run it as a cronjob, it ran but wasn't able to fetch the feeds.
## crontab -e
*/15 * * * * * /home/username/twittercron
The script has been chmod +x. Not sure why it's as such. Any ideas?
Ruby Version Manager (rvm) was causing the problem. I had to call the script in cron like this.
*/15 * * * * bash -c 'source /home/username/.rvm/scripts/rvm && /usr/bin/env ruby /home/username/twitter/twitter.rb friends'
I think that can be done just by forcing bash to act as a login shell, so it sources .bashrc, etc.:
*/15 * * * * * /bin/bash -l -c '/home/username/twittercron'
Here is an article about this.
I've had good experience using http://github.com/javan/whenever
It uses a Ruby DSL to manage cron tasks and it handles setting all the environment magic.
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With