Creating a Daemon in Debian Linux
There have been plenty of times where I want to implement a simple daemon in Debian Linux. I know, I know… I could just install a Debian package and have the daemon created for me (if it’s included). There are times, though, where I want to compile the latest version of an application (like Asterisk). Also, I might want to compile darkice with lame support. (Darkice only has ogg support in the Debian Sarge package). Preferably, I’d like to create a daemon which sets mixer levels then runs darkice. I’ve found various tutorials and howto docs on the Internet but they haven’t been simplified enough. This post will outline a straightforward way to create a daemon in Debian Linux. In this example, I am running Debian Sarge.
Creating the Init Script
Debian has a skeleton daemon script that you can modify: /etc/init.d/skeleton. Let’s modify this init script to adjust the mixer and run darkice as a daemon. First, create a copy of the skeleton script and name it to something relevant to your to-be-daemonized application. We’ll use darkice as the example.
cp /etc/init.d/skeleton /etc/init.d/darkice
Set the Variables
Edit /etc/init.d/darkice with your favorite Linux editor. At the top, modify the comments section to describe the daemon, author, and version. Below the comments section, change the DESC and NAME variables. NAME is the executable file of the program (the NAME variable [as $NAME] is used throughout the script). Also, set the proper path for the DAEMON variable. For my /etc/init.d/darkice dameon, I set the variables like this:
DESC="Darkice daemon"
NAME=darkice
DAEMON=/usr/local/bin/$NAME
Change the Start Case
Under the comment, “# Function that starts the daemon/service” there is a line that begins with “start-stop-daemon –start”. This line needs to be modified to run the executable. For my darkice example, I changed this line to:
start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- -c /etc/darkice/darkice.mp3.cfg -v 0 &> /dev/null
Darkice takes some arguments; in this case, everything after the last “--“. “-c /etc/darkice/darkice.mp3.cfg” is for specifying the configuration file. “-v 0” turns verbosity off, and “&> /dev/null” makes any text that’s returned by darkice to go to /dev/null so no one sees it.
Extra Command(s) at Start
When the dameon starts, I also want it to set some mixer settings. In my case, I have aumix set the volume level to 75 and the record input to line. This statement goes right above the “start-stop-daemon –start” line:
aumix -l75 -lR
Change the Stop Case
Under the comment, “# Function that stops the daemon/service” there are two lines:
start-stop-daemon --stop --quiet --pidfile $PIDFILE \
--name $NAME
Replace those two lines with the following line:
kill `cat $PIDFILE`
This line will kill the program based on its pid (via the PIDFILE variable). As a side note, the trailing “\” on the first line only means that the statement continues on the next line. This is useful for making a long statement look pretty.
After making these edits, save your daemon file and quit the editor. Since the daemon was copied from /etc/init.d/skeleton, all the appropriate file permissions are intact.
Install the Daemon
The update-rc.d command creates the proper symlinks for the daemon so it will start at boot time and stop at shutdown. For the darkice example, I ran the following command. I specified “99″ so the daemon will start last at bootup.
update-rc.d darkice defaults 99
Testing the Daemon
You can try starting and stopping the daemon with (in the darkice example) “/etc/init.d/darkice start” and “/etc/init.d/darkice stop”. You can also restart your Debian box and make sure your daemon loads at boot. One way to check is with the ps command. A “ps -A | grep darkice” command will display any running processes that contain “darkice”.
Removing Symlinks to the Daemon
If you want to stop the daemon from starting at boot and stopping at shutdown, use the following command. (This is with the darkice example.)
update-rc.d -f darkice remove
The “-f” switch forces the removal if the /etc/init.d/darkice daemon script still exists.
Thanks to the debianHELP Wiki for help in creating this post.
April 3rd, 2008 at 4:25 am
Thanks a lot that really helped me!!
April 11th, 2008 at 10:26 am
Great tutorial, really! Simple and sweet! 10x a lot
October 9th, 2008 at 6:00 am
man update-rc.d
>>>
A common system administration error is to delete the links with the
thought that this will “disable” the service, i.e., that this will pre-
vent the service from being started. However, if all links have been
deleted then the next time the package is upgraded, the package’s
postinst script will run update-rc.d again and this will reinstall
links at their factory default locations. The correct way to disable
services is to configure the service as stopped in all runlevels in
which it is started by default. In the System V init system this means
renaming the service’s symbolic links from S to K.
<<>>
To disable a script in this directory, rename it so that it begins with
a ‘K’.
<<<
Cheers!
bkil