ChrisCodes.com

Using Ruby to mimic Red Hat's service utility

I'm a Debian user, and prefer Debian based systems. However, at my new job I started a few months back, they have a few servers running RHEL. I've gotten used to simply typing service lighttpd restart, and keep wanting to type that on my Debian based servers.
So, to scratch that itch, I wrote a few lines in Ruby to give a crude implementation of it.

Create the file /usr/local/bin/service containing the following.


#!/usr/bin/env ruby

service = '/etc/init.d/' + ARGV[0]
action = ARGV[1]

if service && action
  File.file?(service) ? system("#{service} #{action}") : nil
else
  puts "Usage: service servicename action"
end

Then simply make it executable.

sudo chmod +x /usr/local/bin/service

Now you can call service servicename start, instead of the lengthy /etc/init.d/servicename start that's typical on a Debian based system. That's 12 characters less to keep you more productive! eg, to restart ssh type

sudo service ssh restart

Yes, it coould be more robust, but it suits my needs.

If you're need is to have your Ruby in "one liner style", this version may suit you better.


#!/usr/bin/env ruby

service = '/etc/init.d/' + ARGV[0]
action = ARGV[1]

(service && action) ? (File.file?(service) ? system("#{service} #{action}") : nil) : (puts "Usage: service servicename action")

Chris keyed this in on: 2007-04-29
Filed in: Debian, Linux, Ruby

Feed Icon

Copyright © 2009 Chris Martin