How do I schedule a Ruby program to run?

software development

#1

Hello all,

I’ve searched the forums and haven’t found any info on this… I have a Ruby program (not Rails ~ just a plain old .rb file). I can run this program by calling “ruby myprogram.rb” within SSH and it runs how it should. I want to set it up to run several times a day (it’s scraping a website and generating an RSS feed for me).

How can I set this up to be on a schedule to run? Can I use cron? I’m under the impression that cron can only be used for Perl programs, etc? Any help would be great!

Thanks!


#2

Yup, cron is the way to go. I use cron for a scheduled task in my Rails app, but the task itself is just some ruby code. A typical crontab entry could look like this:

0 0 * * * ruby /home/your_site/path/to/script/script.rb

This would run the script every day at midnight. I don’t know how familiar you are with cron, so if you don’t understand that entry, here’s a website with a lot more details: http://www.computerhope.com/unix/ucrontab.htm


#3

That’s awesome… Exactly what I was looking for. I was just missing the “ruby” in front of the path to my whatever.rb file…

Thanks!


#4

You can also add a ‘shebang’ line to your script. Just add this as the first line of the script:

#!/usr/bin/ruby

and use the command chmod +x your_script.rb

You can then run the script directly with cron.