rubycronubuntu-11.10

Crontab with Ruby script returning error code 1


I'm rewriting some shell scripts in Ruby that will be used to backup specific content from our site. I am running Ruby 1.8.7 and have the script running locally as root and it works fine.

I have just attempted to add the script to cronttab on Ubuntu 11.10.

*/5 * * * * ruby /root/code/backup_images.rb local

When the cron runs I receive the following error in the syslog:

Mar 21 16:15:01 ubuntu CRON[4942]: (root) CMD (ruby /root/code/backup_images.rb local)
Mar 21 16:15:02 ubuntu CRON[4941]: (CRON) error (grandchild #4942 failed with exit status 1)

I've tried the following solutions but to no avail:

env -i $SHELL --norc

Which supposedly creates a new naked shell that mimics what crontab runs and this works fine with my script.

#!/usr/bin/env ruby

I have added that declaration to the top of my script which hasn't changed the functionality when running it manually nor has it resolved the issue with the crontab.

I have even tried to output the results of the call to a file, however that only creates a new file with nothing in it.

I'm not sure what else could be the problem, but I'm assuming it has to be something with Cron not being able to find Ruby or something of that nature. I'm rather new to this, so I'm reaching a dead end here.

Thanks in advance!


Solution

  • I believe the fault is that you need to apply the path to ruby in your crontab

    but personally I prefer just to path the ruby file to be executed.

    */5 * * * * /root/code/backup_images.rb local
    

    and then append

    #!/usr/bin/env ruby  
    

    to the top of backup_images.rb

    you also need to make the ruby file executable with chmod +x