systemd is a modern init system and service manager for Linux, known for its efficient parallelization, robust dependency management, and powerful logging capabilities.
The need to add a new service to systemd can vary, whether you want to ensure a script restarts if it stops or you need to manage dependencies to guarantee the script’s prerequisites are available, systemd provides a straightforward solution for service management.
Here are the general steps for adding a new service to systemd:
- Create the unit file:
touch /etc/systemd/system/YOUR-SERVICE-NAME.service
- Define the service with the necessary parameters, here is a basic example:
[Unit] # Description of the service Description=My Custom Service # Ensure this service starts after the network is up After=network.target mysql.service [Service] # Command to execute when starting the service ExecStart=/path/to/your/script.sh # Restart the service if it stops Restart=always # Run the service as this user User=username [Install] # Start this service when the system reaches multi-user.target WantedBy=multi-user.target
For additional information, I recommend visiting Red Hat’s documentation on systemd unit files.
- Reload systemd:
sudo systemctl daemon-reload
- Enable and start your new service:
sudo systemctl enable YOUR-SERVICE-NAME sudo systemctl start YOUR-SERVICE-NAME
You can check the status of your script by using:
systemcrtl status YOUR-SERVICE-NAME
POC:
Let’s say I have a script (/usr/local/bin/poc.sh) that should write “Hello” to a file (/var/check-service) every 5 minutes:
#!/bin/bash # In seconds timetowait=300 while true do echo "`date` - Hello :)" >> /var/check-service sleep "$timetowait" done
And I want to use it as a service.
For the sake of this example, we’ll call the service “poc”, so the unit file should be named poc.service:
noam@noam:~➤ touch /etc/systemd/system/poc.service
And the contents of it would be:
[Unit] Description=Proof of concept After=systemd-udevd.service systemd-timesyncd.service [Service] ExecStart=/usr/local/bin/poc.sh Restart=always User=noam [Install] WantedBy=default.target
Note that the “After” directive specifies that the script should only be loaded after both “systemd-udevd.service” and “systemd-timesyncd.service” have been loaded. This ensures that the script is executed after these services, considering its file writing and time-checking operations.
Then, we’ll reload systemd:
noam@noam:~➤ sudo systemctl daemon-reload
And enable and start the script:
noam@noam:~➤ sudo systemctl enable poc Created symlink /etc/systemd/system/default.target.wants/poc.service → /etc/systemd/system/poc.service. noam@noam:~➤ sudo systemctl start poc
Finally, we can check the status of the service:
noam@noam:~➤ sudo systemctl status poc ● poc.service - Proof of concept Loaded: loaded (/etc/systemd/system/poc.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2024-05-18 22:29:02 IDT; 2s ago Main PID: 1461092 (poc.sh) Tasks: 2 (limit: 18721) Memory: 548.0K CPU: 3ms CGroup: /system.slice/poc.service ├─1461092 /bin/bash /usr/local/bin/poc.sh └─1461094 sleep 300 May 18 22:29:02 noam systemd[1]: Started Proof of concept.
And it seems like it works just fine, we can even check the /var/check-service file for changes:
Tags: systemd
Leave a Reply