Since the Linux operating system is characterized as a multitasking operating system, a daemon is, by definition, a program that continuously executes as a background process. In short, the execution of this process is not dependent on an active user’s system interaction. A normal system user cannot control the periodic execution of a daemon process.
The naming convention that defines most daemon processes is the one letter ‘suffix’ d. This naming convention makes it possible to differentiate between normal system processes and daemon-powered processes. For example, sshd is a daemon process responsible for the management of incoming SSH connections. Another daemon process example is syslogd. It is responsible for the Linux system logging facility.
In a Linux environment, the launch of daemons is at boot time. Since the Linux system is a perfect Unix clone, an init process qualifies as the parent process to a daemon. To start and stop daemons on your Linux operating system, you first need to access the /etc/init.d scripts directory on your OS.
Common daemons functions
- It enables your system to correctly respond to network requests by associating each request with a compatible network port. A typical network port handled by daemons is port 80.
- Daemons make it possible to run or execute scheduled system tasks. The daemon responsible for this specific task is called cron. It will create a cron job that will handle the periodic execution of your scheduled tasks.
- Daemons also offer a priceless contribution in monitoring the performance of your system. For example, they can check up on the RAID array or hard disk health.
Useful Linux service daemons
- amd: Auto Mount Daemon
- anacron: Boot time execution of delayed cron tasks
- apmd: Advanced Power Management Daemon
- atd: Uses at tool functionality to execute queued jobs
- autofs: works hand in hand with the automounter daemon to facilitate on-demand system devices’ mounting and unmounting
- crond: a daemon that handles task scheduling
- cupsd: a daemon that handles CUPS printing
- DHCP: daemon for both Internet Bootstrap protocol Server and Dynamic Host Configuration Protocol.
- gated: routing daemon responsible for multiple routing protocols. It substitutes routed and egpup
- httpd: a daemon that deals with web servers like Apache
- inetd: daemon associated with Internet Superserver
- imapd: daemon for the IMAP server
- lpd: Line Printer Daemon
- memcached: object caching daemon that is in-memory distributed
- mountd: mount daemon
- MySQL: daemon for the MySQL database server
- named: daemon for DNS server
- nfsd: Network File Sharing Daemon
- nfslock: Since nfsd is associated with file locking services, this daemon can start and stop these services.
- nmbd: daemon for Network Message Block
- ntpd: daemon for the Network Time Protocol service
- postfix: a daemon that serves as a mail transport agent. It is an alternative to sendmail.
- Postgresql: daemon for the Postgres database server
- routed: daemon for managing routing tables
- rpcbind: daemon associated with Remote Procedure Call Bind
- sendmail: a daemon that serves as a mail transfer agent
- smbd: daemon for Samba SMB server
- smtpd: daemon for Simple Mail Transfer Protocol
- snmpd: daemon for Simple Network Management Protocol
- squid: daemon associated with a proxy server for web page caching
- sshd: daemon associated with Secure Shell Server
- syncd: daemon for synchronizing system memory with system files
- Syslog: a daemon that performs system logging
- tcpd: this daemon service wrapper executes access restriction protocols related to the inetd-based daemon services. It implements these restrictions through hosts.allow and hosts.deny.
- Telnetd: daemon for the telnet server
- vsftpd: daemon for very secure File Transfer Protocol
- webmin: daemon for the web-based administration server
- xinetd: daemon associated with Enhanced Internet Supervisor
- xntd: daemon for Network Time Server
Whether you are a beginner, intermediate, or expert user in the Linux operating system world, you will never fail to familiarize yourself with either of the above-listed daemons as you advance your skills and expertise within this OS environment.
Starting/stopping/restarting daemons: the terminal-based approach
Now that you have a list of useful Linux daemons to memorize and explore, the first thing you need to know is how to start, stop, or restart these daemons. With your Linux Terminal launched, consider the following syntax rules to start, stop, and restart a daemon on your Linux Operating System.
service preferred-daemon-name start service preferred-daemon-name stop service preferred-daemon-name restart
Replace the preferred-daemon-name syntax argument with the Linux system daemon name of your choice. You can pick one from the daemon list highlighted above as long as it is active or already defined on your Linux system. For example, we can implement the practical usage of the above syntax by trying to start, stop, and restart a daemon. Navigate to the /etc/init.d directory on your terminal for the list of available daemons on your Linux system.
Listing your Linux systems’ daemons
A more effective way of noting the available daemons on your Linux system instead of navigating to the /etc/init.d directory is to list all the defined active and inactive daemons from that directory with a single command. The following command is effective in achieving this objective.
$ service –status-all
The braced positive [+] and negative [-] signs preceding the listed daemon names imply that they are either active or inactive, respectively.
Working with user-defined daemons
Specific rules or protocols must be followed to create and implement a user-defined daemon successfully. These protocols help you to comprehend the execution of daemons on any Linux environment fully. Daemons are also flexible enough to interface with kernel modules through system calls. This daemon functionality stretch supports its interaction with hardware devices like PDAs (Personal Digital Assistants), printers, and viable external controller boards. The building blocks of daemons also contribute to the characteristic power and flexibility of the Linux operating system.
A relatable daemon implementation using Python is carefully demonstrated and documented by Sander Marechal. Be keen to follow the execution order in creating this daemon. First off, your Linux system needs the installation of Python packages to develop daemons successfully. To install Python, you can use the following command.
$ sudo apt install python3-pip python3-dev
The link to Sander Marechal’s authoredPython daemon code also offers a refined Python 3 code version. It would help if you considered implementing it to understand better how daemons work.
If you are unsure whether you have Python installed, run the following command on your Linux terminal.
$ python3 --version
The purpose of any daemon
Since a single daemon is dedicated to handling a specific task, it should execute it to perfection. The task in question can be as simple as creating a report and sending it to an admin through sendmail or as complex as managing multiple domains linked to multiple mailboxes. At some point, the daemon you are going to create will have to talk to other existing daemons.
It is discouraged to have the system user and the created daemon communicate directly. If it is necessary for the daemon, you create to communicate with a system user. This communication can be facilitated through something like a GUI interface. This communication platform can either have GTK+ GUI complexity or signal set simplicity.
Creating your daemon
Numerous approaches support the creation of daemons. For example, you can use your command-line interface to daemonize a Python script as follows:
$ python my_python_script.py &
You can save Sander Marechal’s authored Python3 daemon code on a Python file and daemonize it with the above command. While the above terminal command will easily create a daemon for you, you will have to deal with challenges such as unpredictable terminal outputs. These challenges depend on how well you refactored your Python daemon code. Also, the above approach does not support assigning PID lock files to specific daemons. It makes it impossible to control any daemon as most of them will be executing instantaneously. On the other hand, if you only need a simple daemon, the above-mentioned approach will give you the desired daemon results.
Daemon basic structure
Before a daemon executes or performs an intended function, it will have to consider some preconceived rules leading to its execution. You can think of these rules as low-level housework leading to its actual task. These rules can be broken down into the following steps.
- Creating a fork from a parent process takes place first
- Changing umask (file mode mask) follows
- Logs are opened for writing
- A unique SID (Session ID) is created
- Execution switches from the current working directory to a secondary location to preserve file integrity
- Standard file descriptors are closed
- Execution of targeted daemon code
More on daemons example implementations can be found on GitHub.