Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script executes successfully in commandline but not as a cronjob

Tags:

bash

ruby

cron

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?

like image 329
JasonOng Avatar asked Mar 08 '10 01:03

JasonOng


3 Answers

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'
like image 177
JasonOng Avatar answered Nov 16 '22 23:11

JasonOng


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.

like image 29
sparrovv Avatar answered Nov 16 '22 21:11

sparrovv


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
like image 34
Daniel X Moore Avatar answered Nov 16 '22 21:11

Daniel X Moore