47

What is start-stop-daemon and how should it be used?

I am trying to automate a particular program to run. Whenever the system starts, the program should run. For that I am writing script in /etc/init.d/ location.

Saurabh
  • 58,805
  • 34
  • 169
  • 222
Rajeev Das
  • 1,503
  • 4
  • 17
  • 21

2 Answers2

61

It is a program to manage the start and stop of system level background processes (daemons). You use it by passing in parameters (such as the pid file to create/check) and command arguments for the process you want to launch.

Then, you do one of two things:

start-stop-daemon -S [other arguments] something

start something, if something wasn't already running. If it was running, do nothing.

start-stop-daemon -K [other arguments] something

stop something. If something wasn't running, do nothing.

The man page provides more information on the various arguments. Typically a template is provided in /etc/init.d/ which has other commands for the init process that controls the running of background processes.


What does it mean?

start-stop-daemon --start --background -m --oknodo --pidfile ${PIDFILE} --exec ${DAEMON} -- ${TARGETDIR}

  • --background = launch as a background process
  • -m = make a PID file. This is used when your process doesn't create its own PID file, and is used with --background
  • --oknodo = return 0, not 1 if no actions are taken by the daemon
  • --pidfile ${PIDFILE} = check whether the PID file has been created or not
  • --exec = make sure the processes are instances of this executable (in your case, DAEMON)
Toby Speight
  • 23,550
  • 47
  • 57
  • 84
Burhan Khalid
  • 152,028
  • 17
  • 215
  • 255
  • Is it universal though ? on debian, it seems to be coming from the dpkg package. I wonder if this is provided by other distros under the same name too, or if they use other tools. – ychaouche May 20 '21 at 14:27
9

Copy the /etc/init.d/skeleton file (to e.g. /etc/init.d/rajeevdaemon or another good name), which is a shell script with a lot of comments, and edit it to suit your needs. Then add appropriate symlinks from e.g. /etc/rc2.d/S98rajeevdaemon and /etc/rc2.d/K98rajeevdaemon to it.

Read more about runlevels.

And recent (or future) Linux distributions are using more and more systemd

Basile Starynkevitch
  • 1
  • 16
  • 251
  • 479