Tutorial to run Python script via systemd
- Write Service file
- place your service files inside
/etc/systemd/system/folder - reload services using
systemctl daemon-reload - after that you are able to perform operations such as
- systemctl start name.service
- systemctl status name.service
- systemctl stop name.service
- systemctl restart name.service
[Unit] # service description Description=**Enter Service Description** After=syslog.target [Service] Type=simple # user and group -- to run service User=**Enter username** Group=**Enter groupname** # project working directory WorkingDirectory=/path/to/working/dir/ # Command to execute when the service is started ExecStart=/usr/bin/python /path/to/python/demo_script.py # Automatically restart the service if it crashes Restart=on-failure # set Python's buffering of STDOUT and STDERR value to systemd, so that output from the # service shows up immediately in systemd's logs StandardOutput=syslog StandardError=syslog [Install] # Tell systemd to automatically start this service when the system boots # (assuming the service is enabled) WantedBy=multi-user.target [Unit] # service description Description=Python Demo After=syslog.target [Service] Type=simple # user and group -- to run service User=nivratti Group=nivratti # project working directory WorkingDirectory=/programming/python/projects/ # Command to execute when the service is started ExecStart=/usr/bin/python /programming/python/projects/python-demo.py # Automatically restart the service if it crashes Restart=on-failure # set Python's buffering of STDOUT and STDERR value to systemd, so that output from the # service shows up immediately in systemd's logs StandardOutput=syslog StandardError=syslog [Install] # Tell systemd to automatically start this service when the system boots # (assuming the service is enabled) WantedBy=multi-user.target -
In unit(Service File) /usr/bin/python or any other path
ExecStart=/usr/bin/python /file/path/python_demo_script.py -
or at first line of python file
#!/usr/bin/python -u [Unit] Description=Python Demo Service WorkingDirectory=**Enter working dir path** ExecStart=**Enter python file path** You might have noticed that the output of our script's print calls did not show up on your terminal. This is because systemd detached the service process from that terminal and also redirected the process's STDOUT and STDERR streams.
[Service] Environment=PYTHONUNBUFFERED=1 As always when you change your unit file you need to tell systemd to reload its configuration, and (if your service is currently running), restart the service:
$ systemctl daemon-reload $ systemctl restart python_demo_service The output from our script should now show up in systemd's logs, which by default are redirected to syslog:
$ grep 'Python Demo Service' /var/log/syslog Dec 30 18:05:34 leibniz python[26218]: Hello from the Python Demo Service Another way to display your service's output is via
$ journalctl --user-unit python_demo_service Many services are intended to be started automatically when the system boots. This is easy to achieve using systemd. First we need to attach our service to a suitable target: targets are special systemd units that are used for grouping other units and for synchronization during startup. See systemd.target for details about targets in general and systemd.special for a list of built-in targets.
For user services, the default.target is usually a good choice. Add the following to your unit file:
[Install] WantedBy=default.target Our service is now ready to be started automatically, but for that to actually happen we have to enable the service first:
$ systemctl --user enable python_demo_service ####To disable autostart, simply disable your service:
$ systemctl --user disable python_demo_service To check whether your service is enabled, use
$ systemctl --user list-unit-files | grep python_demo_service As with any other software, your service might crash. In that case, systemd can automatically try to restart it. By default, systemd will not do that, so you have to enable this functionality in your unit file.
[Service] Restart=on-failure If you moved script file we also need to change the script's location in our unit file: update the ExecStart=... line to
- open the service file
sudo nano /path/to/service_file/service_file_name.py - update the ExecStart=... line
ExecStart=/usr/bin/python /new/path/python_demo_script.py
Manage Systemd Services and Units.
To start a systemd service, executing instructions in the service’s unit file, use the start command.
sudo systemctl start application_name To stop a currently running service, you can use the stop command instead:
sudo systemctl stop application_name To restart a running service, you can use the restart command:
sudo systemctl restart application_name To tell systemd to start services automatically at boot, you must enable them. To start a service at boot, use the enable command:
sudo systemctl enable application_name To disable the service from starting automatically, you can type:
sudo systemctl disable application_name To check the status of a service on your system, you can use the status command
systemctl status application_name