How to create a UNIX /etc/init.d startup script with Ruby
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
September 25, 2006 at 5:35 pm
Excellent! I was just about to delve into doing this to autostart multiple sites using mongrel_cluster, on boot.
September 25, 2006 at 6:23 pm
Tim Morgan has put a basic mongrel_rails one together if you want some reference. See http://bigbold.com/snippets/posts/show/2594
September 25, 2006 at 6:32 pm
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.
September 25, 2006 at 6:55 pm
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?
September 25, 2006 at 7:51 pm
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.
September 25, 2006 at 8:58 pm
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.
September 25, 2006 at 9:07 pm
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.
Pingback: Anonymous
September 26, 2006 at 6:12 am
The daemons library can also be useful for such tasks
September 27, 2006 at 2:06 am
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