Writing a Good Linux Init Script

A good Linux init script that works across multiple distros is hard to find. Most distros have a sample/skeleton init script to base your own init scripts on, but that requires having a separate init script for each distro. For the Stackify Linux agent I wanted to have a single init script that would work on all common Linux distros. I had a decent init script that worked pretty well for ‘start’ and ‘stop’ commands, but not so much for ‘status’. The ‘status’ command worked on my development machine (Ubuntu) but didn’t work on the other distros I was testing on (CentOS/Redhat/Amazon).

Since one of the things the Stackify Linux agent does is provide information on the different installed services and their statuses, it is important that the status of our own service is correct. As we prepare for the first beta release of the Linux agent, I decided it was time to rewrite the init script. I started with some Google searches on how to create a cross-distro init script, but they mostly led me back to the skeleton init scripts. While having lunch with a former coworker at Fuzzy’s Taco Shop – which I highly recommend – we got to discussing init scripts and he mentioned seeing an “init script to rule them all.”

After lunch, I promptly searched for this legendary script. My search lead me to the One Java init script to rule them all, which is an init script for Java application servers (e.g. Tomcat and JBoss) that will work on most Linux distros. Turns out this script was really close to what I needed since the Stackify Linux agent is a Java application. Many Linux distros have started implementing the Linux Standard Base (LSB) which provides logging methods and a standard way to implement init script statuses. However, since not all versions support LSB, I cannot use those logging methods for my init script.

The nice thing about the Java init script to rule them all is that it checks for the existence of the LSB methods and if they do not exist it provides its own implementation. I was able to base the Stackify agent init script largely on this existing script. The script now works on all of the distros previously mentioned and should also work on most other distros. The status returns the correct status on these distros and returns the correct status code for systems that do support LSB.