Creating a service on Linux with systemd. - ArthurLuciani/dataAcquisitionServer GitHub Wiki
Creating a service on Linux with systemd.
One might want to have its python script (or a program) to start automatically each time the system boots up. With systemd it is actually (relatively) easy ! Systemd is the service manager on the recent linux platforms. It enables us to create our own services.
Creating the service file
Create a file photonServerDeamon.service. In this file we want to write
[Unit]
Description=Service de démarage pour le serveur de comptage de photons
After=tlp-init.service
[Service]
Type=simple
ExecStart=/usr/bin/python3 "/home/someone/service/photonServerDeamon.py"
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=photonServerDeamon
User=root
Group=root
[Install]
WantedBy=multi-user.target
Descriptionis what it sounds like: it serves to describe your serviceExecStartis the most important part : it specifies the command that must be executed to start the service; the path must be absoluteRestartspecifies if systemd should restart the service if it ends (eg: unforeseen crash)
Then move the file in the /etc/sytemd/system directory
sudo mv photonServerDeamon.service /etc/systemd/system/photonServerDeamon.service
And make sure that root can read, edit and execute this file
sudo chmod u+rwx /etc/systemd/system/photonServerDeamon.service
Enabling and starting the service
To enable the service use
sudo systemctl enable photonServerDeamon
The service will now be started during the next boot. It can also be started immediately with
sudo systemctl start photonServerDeamon
Debugging
If trouble arise use either
sudo systemctl status photonServerDeamon
to check the service status or
sudo journalctl -xe
to see went what went wrong.
Uninstalling
To disable a service just use
sudo systemctl disable photonServerDeamon
Note that the service file will not be deleted.