Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

How to create a UNIX /etc/init.d startup script with Ruby

By Peter Cooper / September 25, 2006

Most init.d start-up scripts seem to be bash scripts, but you can write them with any language. Here's some template code I use to create my own Linux services. You can even add them to chkconfig to be started properly on startup, and they'll also work with RedHat / CentOS's service system straight off.

(Update: January 2007, John Dewey warns me that this will not work on Solaris as it forces scripts to be run using /sbin/sh .. I guess a way around it would be to have a script in the middle that then calls Ruby, but it's your call :))

#!/usr/bin/env ruby
#
# app_name      This is a startup script for use in /etc/init.d 
# 
# chkconfig:    2345 80 20 
# description:  Description of program / service  
APP_NAME = 'app_name'  
case ARGV.first     
when 'status':     	   
status = 'stopped'         

puts "#{APP_NAME} is #{status}"
when 'start': 		# Do your thang
when 'stop': 		# Do your thang
when 'restart': 		# Do your thang		
 end  unless %w{start stop restart status}.include? ARGV.first
puts "Usage: #{APP_NAME} {start|stop|restart}"
exit 
end

Comments

  1. Jon Maddox says:

    Excellent! I was just about to delve into doing this to autostart multiple sites using mongrel_cluster, on boot.

  2. Peter Cooper says:

    Tim Morgan has put a basic mongrel_rails one together if you want some reference. See http://bigbold.com/snippets/posts/show/2594

  3. Benjamin Curtis says:

    Just in case you missed it, mongrel_cluster comes with an init.d script to start multiple rails app on boot. It's resources/mongrel_cluster in the gem distribution.

  4. Jon Maddox says:

    hmm, will it autodetect my apps? My reasoning for writing a ruby script was that i was going to parse my /home/dev/apps directory with all the sites in it and iterate over each directory grabbing the mongrel conf from each and starting them..

    Am i reinventing the wheel?

  5. Peter Cooper says:

    I can't remember exactly what I did, but I got the regular one to work like that, Jon. However, it didn't 'autodetect' as such.. you have to copy the mongrel config file from your Rails app into a special folder.

  6. Jon Maddox says:

    Yeah, thats the part I dont really want to do. I'd like to take one step out. I think i'll play with this tonight and see what I come up with.

  7. Peter Cooper says:

    Actually, I think I symbolically link the mongrel configs.. so you could probably automate /that/ with some additions to the init.d file. On 'start' just do a clever 'find' for mongrel configs (under a certain directory pattern, with wildcards), symbolic link them.. and then start the clusters.

  8. Pingback: Anonymous

  9. zimbatm says:

    The daemons library can also be useful for such tasks

  10. Jon Maddox says:

    As a follow up, I went ahead and hacked up that script tonight, you can check it out here:

    http://www.simplisticcomplexity.com/2006/9/26/start-and-stop-all-your-mongrel_cluster-processes

Other Posts to Enjoy

Twitter Mentions