Nick | OneThingWell.dev
Posted on April 4, 2022
To put it very simply, a daemon is a service process that runs in the background.
Working with daemons on Linux can be tricky for new users - because of the historical baggage and backward compatibility, there are now many ways to do the same thing.
Just a bit of background first: after the boot loader loads the kernel and everything is initialized, the final step is starting the init
process. This process is also called the root process and it has PID 1 (PID 0 is part of the kernel - the so-called sched
or swapper
process).
systemd
These days, you're most likely using a Linux distribution that is running systemd
as PID 1.
To control the services under systemd, you're mostly going to use systemctl
command.
Checking the status of the service:
systemctl status myservice.service
Starting and stopping the service:
systemctl start myservice.service
systemctl stop myservice.service
systemctl restart myservice.service
Enabling and disabling the service (on every boot):
systemctl enable myservice.service
systemctl disable myservice.service
Adding a new service
To add a new service, you just need to put the Unit file in /etc/systemd/system/ directory.
A minimal /etc/systemd/system/myservice.service could look something like this:
[Unit]
Description=My Awesome Service
[Service]
# Simple service won't do any forking
Type=simple
ExecStart=/path/to/command
[Install]
# Start the service before we get to multi-user mode
WantedBy=multi-user.target
Now we just need to do the "daemon-reload" (to tell systemd to pick up our new file):
systemctl daemon-reload
And enable and start our service:
systemctl enable myservice.service
systemctl start myservice.service
systemctl status myservice.service
SysVInit
systemd
is a relatively new addition to Linux. Traditionally, Linux systems have been using SysVInit as an init subsystem. It's much simpler than systemd
(closer to the original Unix philosophy), and this whole switching thing had brought a lot of controversy to the Linux community. There are still actively maintained distributions that are using SysVInit.
Also, there is still a lot of software and documentation which are using these commands.
"Services" under SysVInit are just scripts inside /etc/init.d/ directory which are responding to arguments like start, stop, restart, etc.
This was the original SysVInit of starting and stopping the service:
/etc/init.d/myservice start
/etc/init.d/myservice stop
/etc/init.d/myservice restart
Later, service
command was added (which was mostly equivalent to the above, but not always):
service myservice start
service myservice stop
service myservice restart
(these commands are backwards compatible - it should work the same as using systemctl
on your systemd-based system)
Adding the service to default runlevels (enabling it on every boot):
# Debian-based distros
update-rc.d myservice defaults
# Redhat-based distros
chkconfig myservice on
Upstart
Upstart was an event-based replacement for SysVInit created by Canonical, but today is not used anymore.
If you somehow run into the system that is still using it, you can mostly use the same, above-mentioned commands for SysVInit.
Note: this is a snapshot of (WIP) topic from the Understanding Simplicity wiki. All suggestions (and reactions) are welcome. You can find the latest version here: Linux: Working With Daemons
Posted on April 4, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
December 19, 2020