2

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!

Kiran
  • 20,167
  • 11
  • 67
  • 99
Nick
  • 103
  • 1
  • 1
  • 12

2 Answers2

0

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

tomodachi
  • 251
  • 3
  • 9
  • Well I didn't have execute permission and my pathing was incorrect in the file. The current call is now `*/5 * * * * /root/code/backup_images.rb local` and the response is the same. Exit with status 1 – Nick Mar 22 '12 at 13:47
0

So with the help from @tomodachi I went a step further and explicitly wrote out the following crontab line and it works now.

*/15 * * * * cd /root/code/ && ./backup_images.rb local > /root/code/log/script.log

So basically I tried to execute the command from a bash shell and it wasn't finding the script. So I explicitly go into the direct and then execute the backup script.

Everything worked fine from this point forward.

Thanks of your help.

Nick
  • 103
  • 1
  • 1
  • 12