Unlike Ubuntu's Upstart or newer systems using systemd, Debian traditionally relies on SysVinit scripts for daemon management. However, you can still create manual start/stop control without relying on boot-time execution.
Debian provides start-stop-daemon
- the standard way to handle daemon processes. It handles PID file creation, process management, and user privileges. Here's a basic implementation:
#!/bin/sh
# /etc/init.d/custom-daemon
DAEMON="/usr/bin/my_program"
DAEMON_OPTS="--config /etc/my_program.conf"
PIDFILE="/var/run/my_program.pid"
NAME="my_program"
case "$1" in
start)
echo -n "Starting $NAME: "
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
echo "done."
;;
stop)
echo -n "Stopping $NAME: "
start-stop-daemon --stop --quiet --pidfile $PIDFILE
echo "done."
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
;;
esac
exit 0
After creating the script:
sudo chmod +x /etc/init.d/custom-daemon
To use your daemon without automatic startup:
sudo update-rc.d custom-daemon disable # Prevent automatic start
sudo service custom-daemon start # Manual start
sudo service custom-daemon stop # Manual stop
For newer Debian releases with systemd:
[Unit]
Description=My Custom Daemon
[Service]
ExecStart=/usr/bin/my_program --config /etc/my_program.conf
PIDFile=/var/run/my_program.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
Save as /etc/systemd/system/my_program.service
then:
sudo systemctl daemon-reload
sudo systemctl start my_program
When creating your own implementation, ensure proper PID file handling:
# In your application:
with open("/var/run/my_program.pid", "w") as f:
f.write(str(os.getpid()))
While Ubuntu's Upstart was once popular, modern Debian systems primarily use systemd for service management. However, the traditional SysV init system (/etc/init.d) remains available. For manually controlled daemons, both approaches are valid.
Create a custom service file in /etc/systemd/system/:
[Unit] Description=My Custom Daemon After=network.target [Service] Type=simple ExecStart=/usr/local/bin/my_daemon --option=value PIDFile=/var/run/my_daemon.pid Restart=on-failure [Install] WantedBy=multi-user.target
Control commands:
sudo systemctl start my_daemon sudo systemctl stop my_daemon sudo systemctl status my_daemon
For those preferring SysV style, create /etc/init.d/my_daemon:
#!/bin/sh ### BEGIN INIT INFO # Provides: my_daemon # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Custom daemon ### END INIT INFO DAEMON="/usr/local/bin/my_daemon" PIDFILE="/var/run/my_daemon.pid" OPTIONS="--option=value" case "$1" in start) start-stop-daemon --start --quiet --background \ --make-pidfile --pidfile $PIDFILE \ --exec $DAEMON -- $OPTIONS ;; stop) start-stop-daemon --stop --quiet --pidfile $PIDFILE ;; *) echo "Usage: /etc/init.d/my_daemon {start|stop}" exit 1 ;; esac exit 0
Make it executable and control it:
sudo chmod +x /etc/init.d/my_daemon sudo /etc/init.d/my_daemon start sudo /etc/init.d/my_daemon stop
For quick manual control without full service files:
# Start start-stop-daemon --start --background --make-pidfile \ --pidfile /var/run/my_daemon.pid \ --exec /usr/local/bin/my_daemon -- --option=value # Stop start-stop-daemon --stop --pidfile /var/run/my_daemon.pid
For modern systems, systemd offers better logging and integration. The init.d method remains useful for compatibility. The direct start-stop-daemon approach works well for temporary daemons during development.