Adding Rails cron jobs on Dreamhost
Okay, so I thought adding a cron job for my RoR application would be pretty easy. Having never done it before I started hitting the search engines to the how-to’s. I found this to be a lot harder than I thought due to some technical difficulties (on my part) so I want to detail how i FINALLY got it done.
First, http://wiki.dreamhost.com/index.php/Crontab is dreamhost’s writeup about crontab and it has mostly what you need for the syntax of each line in the crontab.
Here’s what I learned, a crontab holds a list of all the cron jobs you want to perform. At first i tried to create a local .txt file on my computer and ftp it up to the server. Due to the .txt file’s format and the fact that I’m using Dreamweaver for FTP (which I would not recommend), something did not work; something about ASCII format and the way it uploads the file (and also something about the last character in the file has to be cr\lf) <- dont worry about that.
So, this is how I got it done.
- I SSH’d to my dreamhost account (you can get there right?).
- Then type crontab -l to list the contents of your crontab file. If this is the first you’ve done of this kind of thing it will say that you dont have a crontab file.
- Next you need to change the default text editor to vim. Nano is the default editor for dreamhost and it will not work for creating a crontab. it has something to do with the way it writes an asterick (*). Type export EDITOR=vim to change the editor to vim. Note: I am horrbile with the commands for using vim as an editor and found this cheatsheet helpful: http://www.lagmonster.org/docs/vi.html
- Now you need to create the crontab file. Do so by typing crontab -e
- Ok, press ‘i’ to get into “insertion mode” in the text file. In my crontab the first line i specify my email address to send errors to. This is optional. If you want it, type MAILTO=”youremailaddress@somewhere.com” in the first line and press enter.
- We’re now going to add our first cron job to this file. Go to the link above to understand the components of the line you will be adding. In my crontab my first cron job looks like this
59 2 * * * ~/listyourwish.com/script/runner -e development 'ScheduledNotifications.send_out_notifications' > /cronlogs/notifications.log. This is telling crontab to run this job at 2:59am every day of every month of every year. A couple fo things to note here; the “~/” portion of the path referrs to your user’s home directory (/home/username). And the “> filename.log” tells crontab to write errors to that log file. Note about Ruby on Rails: in order to run a Ruby on Rails script from command line (cron) you must have the path of the script runner (command line ruby compiler ??) in the cron job line. It is located in your RoR app under script/runner - if you want more than one script to run at different times then add a line for each script just like we did in the previous example.
- when you are done adding them, press the escape [ESC] key to exit out of “insertion mode”, then type
:x[enter] to write and save your crontab. - make sure your crontab is active by typing crontab -l to list it’s contents and you should see the new file you just saved
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]