Recently I needed to make a server application run as an Unix daemon and be able to start, stop and restart it on demand. The application I’m talking about didn’t have any startup/shutdown utilities analogous to for example Tomcat. It run as a script, without even detaching from the console. At first I figured I’ll have to write a utility application that would start the process, store it’s PID in some file and somehow “daemonize” the forked process. So as always before doing anything, I decided to check if anyone hasn’t done that before me. I wasn’t really surprised when a quick Google search allowed me to find a utility written by someone just for that and even more. The application is called start-stop-daemon and should be installed by default on almost any Linux (well, it certainly was on my Ubuntu 7.10).

Below is a quick tutorial on how to start any server application as a Unix daemon on system startup. It was tested on Ubuntu Linux version 7.10 (Gutsy Gibbon), but it should work on most Linux distributions, and certainly on those which are Debian based.

Let’s assume that the application we want to run is /path/to/script/app.sh

First, we need to create a script in /etc/init.d/ Let us name the file /etc/init.d/app

The file contents would look something like this:


#!/bin/sh

PATH=/bin:/usr/bin:/sbin:/usr/sbin

case "$1" in
start)
echo "Starting application"
start-stop-daemon -d /path/to/script -c username --start -b -m -p /path/to/pid/app.pid --exec /path/to/script/app.sh
echo "Started."
;;
stop)
echo "Stopping Application"
start-stop-daemon -d /path/to/script --stop -p /path/to/pid/app.pid
rm -f /path/to/pid/app.pid
echo "Stopped."
;;
restart)
sh $0 stop
sh $0 start
;;
*)
echo "Usage: app {start|stop|restart}"
exit 1
;;
esac

exit 0

This is a pretty standard service startup/shutdown file. The most interesting part of this script is the use of the start-stop-daemon application. This line:

start-stop-daemon -d /path/to/script -c username --start -b -m -p /path/to/pid/app.pid --exec /path/to/script/app.sh

starts the process as a daemon. The -b option makes the process run in background, the -m option is used to create a PID file, -p specifies where the PID file is located (the directory where the file is located must already exist, as it won’t be created automatically), -d specifies the current directory that will be seen by the script, -c changes the process owner. The start-stop-daemon application will check for PID file when starting, and won’t start the script if process with this PID exists. If the script for some reason fails to start, or encounters an error, the PID file won’t be deleted, so keep that in mind. This technique is certainly not for general-purpose usage - it should be used only when you don’t have any other way to properly startup your server (or maybe if you are just looking for a quick-and-dirty solution).

I figure that the line that stops the daemon does not need more explanations. Just note that the PID file is deleted manually after stopping the service - start-stop-daemon won’t do that for you.

Now we just need to make the application automatically start at system startup:

sudo update-rc.d app defaults

You can start/stop/restart the server the same way you would do it with for example Apache:

sudo /etc/init.d/app (start|stop|restart)

Hope you’ll find this trick helpful:)

Psyho

Share/Save/Bookmark

Posted in linux at April 19th, 2008. by Adam 'Psyho' Pohorecki 2 Comments.