Keeping passenger apps live with cron
-Wednesday, April 15, 2009 By: Jon Kinney
Like many of the “cool kids” in the rails world, I have started using passenger for hosting most of my rails apps on my personal server. One of the great features is that passenger has the ability to “spin up” more instances to a site that is getting a lot of page requests, while not paying as much attention to a site that isn’t receiving any traffic at the moment. This load balancing works great for me for the most part, but one disadvantage is that if a site is left too long without a request it can be very slow to get the site “spun up” again. That is where our new friend cron can help.
Cron may not be installed on your system (it wasn't on my minimal version of ubuntu 8.04 LTS) so if that is the case just grab it from the repositories.
1 sudo apt-get install cron
Once that is done you can edit your cron jobs by entering the crontab. At the command prompt type:
1 sudo crontab -e
Creating cron jobs can be confusing without a map of the stars... but luckily one is available at wikipedia here: link to http://en.wikipedia.org/wiki/Cron
1 .---------------- minute (0 - 59)
2 | .------------- hour (0 - 23)
3 | | .---------- day of month (1 - 31)
4 | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
5 | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
6 | | | | |
7 * * * * * command to be executed
So that's not too bad... each * corresponds to a time entity, minute, hour, etc... and for most of the cron jobs I run I need them to happen multiple times per day so we're only dealing with the first 1 or 2 stars, the rest need to remain * because that means "every".
Here is a cron job that I use to keep passenger "fresh", meaning not spin down, for this site.
1 */10 * * * * wget -q http://jonkinney.com
Notice that everything is * except for the minute space. That is because I want to run this cron job every 10 minutes. The interesting thing I've noticed from watching the logs is that cron doesn't care when you create the job, the timing isn't relative... it's absolute and in this case on the 10 minutes of each hour. So if you create the cron job at 8:05 AM, then the next time it will run is 8:10, then 8:20, and so on.
I am also running sphinx on my VPS and need to issue a command to re-index the delta and main. I want to do the delta every 10 min, and the main on the first minute of each 4th hour. I also want to check that sphinx is started constantly so I have a job to check that every 3 minutes. The jobs are specified below:
1 */10 * * * * bash -c 'cd /var/www/apps/jonkinneydotcom/current/; /usr/bin/rake ultrasphinx:index:delta RAILS_ENV=production'
2 1 4 * * * bash -c 'cd /var/www/apps/jonkinneydotcom/current/; /urs/bin/rake ultrasphinx:index:main RAILS_ENV=production'
3 */3 * * * * bash -c 'cd /var/www/apps/jonkinneydotcom/current/; /usr/bin/rake ultrasphinx:daemon:start RAILS_ENV=production'
News & Events
Tech Review: Web Design For Developers - June 2009
A friend of mine in the web development community is releasing a book called "Web Design for Developers: A Programmer's Guide to Design Tools and Techniques". I was asked to do a tech review of the book and will be sharing my thoughts as well as some cool info presented in the book in a short series of upcoming blog posts. If you want to grab a beta copy of the book head over to my favorite publisher The Pragmatic Programmers.
First Class Audio Production - March 1st 2009
My most recent studio project was mixing and producing the latest a cappella album to come out of Eau Claire, WI. Until Proven Guilty is the Innocent Men's 4th studio album and marks a huge leap forward in recording and production quality for the group. Check back for demos of the mastered songs very soon! http://theinnocentmen.com.
Rate This Post: 0.0 (average)


