Jan 112015

There are scenarios out there where you’d like to run a command as a linux service. Maybe you have a script you would like to run every time Linux is started, who knows. In my case, I had written a Python package which started a web server to listen for HTTP posts and then did something specific with them. It needed to be started every time the machine was restarted. I was using Fedora 16 at the time, not sure how this will hold up on other Linux distros!

Luckily, Linux has a special command, chkconfig,to take some of the pain way. Straight out of the man page:

chkconfig provides a simple command-line tool for maintaining the
/etc/rc[0-6].d directory hierarchy by relieving system administrators
of the task of directly manipulating the numerous symbolic links in
those directories.

There was some debate about what RC actually stands for, https://bbs.archlinux.org/viewtopic.php?id=13052 , but it seems the consensus is run commands. Here is essentially the script that I ended up using, but of course replacing name_of_service and my_command. The important thing is that my_command is visible to root, i.e., my_command is located somewhere like /usr/bin.  In the script we have:

Provides: name_of_service

I’m not sure if it’s necessary, but I also chose this as the name of the script.

The first thing you need to do is place a script like the one above in /etc/init.d./name_of_service

Since I knew what I chose for “my_command” was unique and would only ever be run once, the way I defined stop() works fine. However I don’t like it and I think I’ll refactor that soon. There should be no guess work into what running “service name_of_my_service stop” will actually end up doing. After a bit of digging, there is a way using the PID generated by the service:


I also knew that there was possibilities of my tool exiting with an error when attempting to start/stop/restart (things like permissions, etc), which is why RETVAL=$? is called after each command. If any of those 3 commands are called and exit with a non-zero status, then the entire call will indicate a fail.

Moving forward, we have Linux manage the service:

Done! You could now run:

But if you wanna learn a bit more about these portions:

Check out the references. Essentially we have what we would like to be loaded by the operating system prior to starting our service, and then define the default run levels of the service.


  1. http://www.thegeekstuff.com/2012/03/lsbinit-script/
  2. http://linuxcommand.org/man_pages/chkconfig8.html

  One Response to “Making a command line tool into a Linux service”

  1. […] made a previous post about installing a command line tool as a Linux service, so you could go that route. But if you’d prefer to just use it on the fly then you could do […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">