Daemon management in Linux is a fundamental skill for any system administrator. These background processes allow our operating system to run efficiently, executing critical tasks without direct user intervention. Mastering daemon creation and management will give you deeper control over your Linux system, allowing you to automate tasks and create custom services that run according to your specific needs.
What are daemons in Linux and what are they for?
Daemons are a mechanism that Linux has to give service behavior to processes. This means that we can configure them to start automatically when the system boots, under specific conditions and with predetermined behaviors.
These services are created through configuration files called "Unit Files", which define how and when a daemon should be executed. A common use case is when we need a script (for example, in Python) to run constantly performing a specific function without manual intervention.
The first and most important daemon in Linux is SystemD, which functions as the main manager for all other system daemons. SystemD is responsible for starting critical components such as:
- Network drivers
- Graphical environment
- Essential operating system services
To interact with SystemD, we use SystemCTL (System Control), which provides an interface to manage system daemons and services.
How to create and configure a custom daemon?
To demonstrate the process of creating a daemon, let's implement a simple Python script that works as a basic logger, recording the current date every second in a file.
Preparing the script
First, we need to create our Python script:
import timefrom datetime import datetime
while True: file = open('/date_logs/timestamp.txt', 'a') file.write(f "Timestamp: {datetime.now()}") file.close() time.sleep(1)
This script simply:
- Opens a file in append mode.
- Writes the current date and time
- Closes the file
- Wait a second before repeating the process
Setting up the environment
For our script to work correctly, we must:
- Verify that Python is installed:
python3 -V
- Create the directory where the logs will be saved:
mkdir /date_logs
- Create a directory for our scripts:
mkdir /root/scripts
- Save our script in the appropriate location:
vim /root/scripts/logger.py
Creating the Unit File
The Unit File is the configuration that SystemD will use to manage our daemon. We must create it in the correct location:
vim /etc/systemd/system/system/loggerPython.service.
And add the following content:
[Unit] Description=PythonLogger After=multi-user.target
[Service]Type=simpleRestart=always ExecStart=/usr/bin/python3/root/scripts/logger.py
[Install] WantedBy=multi-user.target
This configuration file has three main sections:
-
[Unit]: Contains metadata and dependencies.
- Description: A simple description of the service
- After: Indicates that this service should be started after the specified target.
-
[Service]: Defines the behavior of the service
- Type: Specifies the type of process (simple, forking, oneshot, etc.)
- Restart: Determines when to restart the service (always, on-failure, etc.)
- ExecStart: The command to be executed to start the service.
-
[Install]: Configures how the service is installed
- WantedBy: Specifies in which target this service should be included.
Activating and managing the daemon
Once the Unit File is created, we must reload SystemD to recognize the changes:
systemctl reload
To enable the service to start automatically at system startup:
systemctl enable loggerPython.service
To manually start the service:
systemctl start loggerPython.service
To check the status of the service:
systemctl status loggerPython.service
To stop the service:
systemctl stop loggerPython.service
To disable automatic startup:
systemctl disable loggerPython.service
How do targets and runlevels work?
In SystemD, targets are similar to runlevels in traditional SysV init systems. The multi-user.
target we use in our example is activated after all essential services (network, drivers, etc.) have been started, allowing access to a text console.
Targets define specific points in the boot process and determine which services should be active at each stage. This allows a more granular and flexible management of system services.
Mastering the creation and management of daemons in Linux will allow you to automate tasks, create custom services and have more control over your system. Have you created a custom daemon for a specific task? Share your experience in the comments and explore other possibilities to take advantage of this powerful Linux feature.
Want to see more contributions, questions and answers from the community?