The content of the article
Despite many problems and conflicting user feelings, systemd has already become the de facto standard on most Linux distributions. Using it, you can create a simple service in a couple of minutes and a dozen lines of settings. At the same time, many more interesting features are documented in a not very clear language or require in-depth knowledge of the intricacies of systemd.
If you have never done your services, let's start with the basics. Systemd operates on abstract units, which are of various types, can provide various resources (processes, sockets, abstract "targets") and require other resources to run.
The most common type of resource is a service. Files with descriptions of services and everything else are in the directory
/lib/systemd/system/. In order for systemd to find a new service, just put your file in this directory. If this service did not exist before, systemd will read the file and load it into memory. However, if you are editing a file of a previously launched service, do not forget to force systemd to re-read the files with the command
sudo systemctl daemon-reload!
Services like oneshot – down with rc.local
Once the standard way to add command execution to the system boot was to add them to /etc/rc.local. The obvious drawback is that there is no way to track how successfully they are completed. In systemd it is easy to create your own service such as oneshot for this purpose, and it can be managed through systemctl, like any other. In this case, systemd will execute the command and consider the service launch successful if it ended with code zero.
Save the following file to
(Unit) Description=Dumb test(Service) ExecStart=/bin/true Type=oneshot(Install) WantedBy=multiuser.target
No additional steps are required, and now you can do with it all the same as with system services: run with
sudo systemctl start dumb-test.serviceput on boot using
sudo systemctl enable dumb-test.service and so on.
We make service from any program
Any long-lived process can be easily turned into a service using the option
Type=idle. In this case, systemd will intercept standard I / O streams and will monitor the life of the process.
To demonstrate, we will write a Python program that simply displays a message in an infinite loop. Save to
import timewhile True: print("Test service is alive") time.sleep(5)
Then create a service file for it in
(Unit) Description=Smart test(Service) ExecStart=/usr/bin/python3 -u /usr/local/bin/test.py Type=idle KillMode=process SyslogIdentifier=smart-test SyslogFacility=daemon Restart=on-failure(Install) WantedBy=multiuser.target
Now you can start our service and make sure that it works:
Continuation is available only to participants
Materials from the latest issues become available separately only two months after publication. To continue reading, you must become a member of the Xakep.ru community.
Join the Xakep.ru Community!
Membership in the community during the specified period will open you access to ALL Hacker materials, increase your personal cumulative discount and allow you to accumulate a professional Xakep Score!
I am already a member of Xakep.ru