tags:

views:

307

answers:

6

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?

A: 

You need to supply the absolute path to the executing command (or via env).

*/15 * * * * * /usr/bin/env sh /home/username/twittercron

Alternatively:

*/15 * * * * * /usr/bin/env ruby /home/username/twitter/twitter.rb friends
bojo
that's a bash script he is running.
ghostdog74
I've tried your alternative suggestion. It's still no good...
JasonOng
What are you expecting as a result? Does it write out to a file or database? Or are you just waiting for the crontab's stdout to be system mailed to you?
bojo
A: 

You have one too many *s in your crontab.

Chris Jester-Young
Sorry it was a typo. I've got correct no. of *'s.
JasonOng
A: 

check your mail and see if there's any error messages. Put some debugging in your bash script, eg echo done>debug.log at the last line and see if debug.log is created. remove the ## /home/username/twittercron in your script and put #!/bin/bash on the first line.

*/15 * * * * * /bin/bash /home/username/twittercron
ghostdog74
Instead of debug.log, try: `[email protected]` (inside the crontab), then running `/bin/bash -x /home/username/twittercron` as the cron command. That will email the full trace at the end.
Chris Jester-Young
JasonOng
+5  A: 

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'
JasonOng
I've been trying to solve this problem for about a week now. Thank you so much for this!
Eric Koslow
You're most welcomed.
JasonOng
kch
A: 

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
Daniel X Moore
A: 

I think that it could be done simpler by loading bash all environment -l parameter:

*/15 * * * * * /bin/bash -l -c '/home/username/twittercron'

Here is an article about this.

sparrovv