18

I need to create a daemon from the application in Debian. Is there any standard tool for this in Debian like "upstart" in Ubuntu? I need only start-stop commands, to start a program as a daemon with some options and a pid file and kill it with pid file.

I looked at init.d but it seems these are for boot-time launch. I want to start my daemon manually.

Kuroki Kaze
  • 345
  • 2
  • 5
  • 18
  • 2
    Note that this has changed for debian jessie, which has switched the default from sysvinit to systemd. – Jules Apr 05 '16 at 00:30
  • debian jessie appears to have some form of backwards compatibility, at least using /usr/bin/service - if there is no upstart config (/etc/init/{service}.conf) and there's a sysvinit config (/etc/init.d/{service}), then /usr/bin/service will interact with /etc/init.d/{service} as before under sysvinit. – Kevin Mar 14 '17 at 19:55

3 Answers3

19

You can create your daemon manually following the /etc/init.d/skeleton file on Debian.

You can use /usr/bin/service to launch $ sudo service yourdaemon start and sstop $ sudo service yourdaemon stop your daemon.

As long as you do not link your script to any of the /etc/rc?.d directories, it won't get started on startup.

On the other hand, you may want to look at daemontools, which is not standard on debian but has some interesting features.

chmeee
  • 7,270
  • 3
  • 29
  • 43
3

Debian (and Ubuntu) have the helper program start-stop-daemon which is used in the init scripts. It has quite a few options to start and track daemons. You can simply write a wrapper around it, e.g.

case $1 in
start) start-stop-daemon --start --exec /my/exec/prog --pidfile /my/pid/file --background
       ;;
stop)  start-stop-daemon --stop --pidfile /my/pid/file 
       ;;
esac
Dan Andreatta
  • 5,384
  • 2
  • 23
  • 14
  • 3
    I would prefer /etc/init.d/skeleton suggested above, because it also includes INIT INFO parts which helps with migration to dependency booting – dpavlin Apr 26 '10 at 18:06
2

To properly daemonize a process for Debian you will need to take several steps including forking away from the controlling process, resetting IO, and creating a process id file to play nice. You can instead use something like the daemon program from the package of the same name to do that for you. If that is what you wanted to do, the answer can be found here on StackOverflow: https://stackoverflow.com/questions/3095566/linux-daemonize

The upstart command is more akin to Debian's invoke-rc.d command or the service command from the sysvinit-utils package. They expect to work off of init scripts in /etc/init.d. It is standard on Debian to create one of those for your package and have it use the start-stop-daemon program as you can see in the /etc/init.d/skeleton example.

Just because you create an /etc/init.d/myservice script doesn't mean it has to start automatically. You can adjust the runlevels at which it stops and starts automatically using a tool like update-rc.d. This is described in more detail at Disable a service from starting at all runlevels?

jla
  • 153
  • 1
  • 1
  • 7