How to Start Mongrels on Boot

Posted on December 17, 2007

Getting your mongrels to startup on boot is one of the last tricky things to do before your Rails setup is completely streamlined.

Using cron and a special @reboot option, the following steps will show you how to do this on a Fedora Core 5 linux system. (should work on similar systems as well)

About my setup—I deploy using a user account, let’s call it ‘foo’, which has a home directory of ’/home/foo’.

But we need to add the main @reboot cron entry as root.

# su to root:
su
# Now edit the root user's cron jobs:
crontab -e
Add the following to the file:
@reboot /root/start_mongrels

Then save and exit—it should’ve installed your new cron job. (use “crontab -l” to confirm)

Now you need to create a file in /root/start_mongrels that will call a helper script that resides under your ‘foo’ (deployment user) account.

# Still as root:
vi /root/start_mongrels

Paste in the following:

#!/bin/sh
su - foo -c "cd /home/foo && ./restart_all.sh" 

Then ensure the file is executable:

chmod 755 /root/start_mongrels

Now exit as root and return to being logged in as the ‘foo’ user. (whichever user you deploy your Rails apps as)

Create your restart_all.sh Script

Create a new file ’/home/foo/restart_all.sh’ with contents like:

#!/bin/sh

# Restart the 'foojiggly' app
pushd /u/apps/foojiggly/current
mongrel_rails cluster::stop
rm -rf log/*.pid
mongrel_rails cluster::start

# Restart the 'funster' app
pushd /u/apps/foojiggly/current
mongrel_rails cluster::stop
rm -rf log/*.pid
mongrel_rails cluster::start

Add as many entries as necessary. Newer versions of ‘mongrel_rails’ come with a ‘—clean’ option that you may want to use instead of the ‘rm -rf log/*.pid’ command.

Next, make sure the ‘restart_all.sh’ file is executable:
chmod 755 /home/foo/restart_all.sh
Now be sure to do a test run to make sure it all works!
sudo /sbin/restart -r 0

Running Sphinx Index Rotator via Cron

Posted on November 13, 2007

Just spent a good 30 minutes to an hour debugging an issue around why Sphinx indexing was running fine via the command-line, but not via cron.

Turns out I’d seen this issue before. Oftentimes scripts running via cron have different paths setup for whatever reason.

So if you are trying to run the command ‘indexer’ it might fail in cron, even though it runs fine in your normal login environment. (full path is: /usr/local/bin/indexer on my system)

Specifying the full path manually did the trick, i.e.:

0-59/15 * * * * /usr/local/bin/indexer --all --rotate --config /foo/current/config/sphinx.conf