开发者

Script executes successfully in commandline but not as a cronjob

开发者 https://www.devze.com 2022-12-22 00:45 出处:网络
I\'ve a bash script that runs a ruby script that fetches my twitter feeds. ## /home/username/twittercron

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 i开发者_运维百科t 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


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


You have one too many *s in your crontab.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消