DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Peter has posted 12 posts at DZone. View Full User Profile

Create A UNIX Init.d Startup Script With Ruby

  • submit to reddit
#!/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		

unless %w{start stop restart status}.include? ARGV.first
        puts "Usage: #{APP_NAME} {start|stop|restart}"


Snippets Manager replied on Sun, 2009/04/05 - 3:40pm

I've written a libray which abstracts the common features of init scripts. You just have to implement a start and stop method. The restart method is generated automatically, but you can set a delay in seconds between calling the stop and the start method. You can get it via "gem install init". Documentation can be found here: An example: require 'init' class Demo < Aef::Init # The time in seconds between stop and start when restart is called stop_start_delay 3 def start # Do you thing end def stop # Do your thing end end # The parser is only executed if the script is executed as a program, never # when the script is required in a ruby program if __FILE__ == $PROGRAM_NAME Demo.parse end