Register Python script as a Linux systemd service

Linux init

” In Unix-based computer operating systems, init (short for initialization) is the first process started during booting of the computer system. Init is a daemon process that continues running until the system is shut down.”

The Linux init system has evolved over time i.e. you might hear about various systems, for example – sysV, upstart, or systemd.
  • system V is the oldest among the 3 mentioned init system:
    • Debian 6 and earlier
    • Ubuntu 9.04 and earlier
    • CentOS 5 and earlier
  • Upstart:
    • Ubuntu 9.10 to Ubuntu 14.10, including Ubuntu 14.04
    • CentOS 6
  • systemd is the init system for the most recent Linux distributions
    • Debian 7 and Debian 8
    • Ubuntu 15.04
    • CentOS 7
Lennart Poettering and Kay Sievers started the project to develop systemd in 2010, and In May 2011 Fedora was the first major Linux distribution to enable systemd as the default init system. As of 2015, most Linux distributions have adopted systemd as their default init system.
As stated in the above definition, init is the first process on boot, hence has the process ID 1.  You can check for the init system in use, by various ways, one way is to get it from process with ID 1:
[nahmed@localhost ~]$ sudo stat /proc/1/exe
File: ‘/proc/1/exe’ -> ‘/usr/lib/systemd/systemd’
Or, use the ps command with PID 1
[nahmed@localhost ~]$ sudo ps 1
PID TTY      STAT   TIME COMMAND
1 ?        Ss     4:08 /usr/lib/systemd/systemd --system --deserialize 17

systemd is the Linux OS deamon with handles all the system service calls i.e. start, stop, enable. Enabling a system service ‘systemctl enable <service-name>’, tells systemd to start the service on reboots. Being a Python developer you might have developed a Python script or application, which was meant to run continuously as a service or daemon (service in the background), for example, a RPC server, message queue consumer, heartbeat publisher, etc, registering it as system service is a good practice. Here I have a RPC server written in Python, I’d like to register as a system service handled by systemd i.e. start, stop, start on boot, etc.

Step 1 – Create The Unit File

Once you have your Python server script ready, next thing you’ll do is create a configuration file (aka a unit file) that tells systemd what we want it to do and when:
$ sudo vi /lib/systemd/system/pyrpc.service
Add in the following text
[Unit]
Description=RPC service at port 57777
After=multi-user.target[Service]
Type=idle
ExecStart=/usr/bin/python /home/nabeel/PycharmProjects/pyrpc .py[Install]
WantedBy=multi-user.target

Step 2 – Set permissions

The permission on the unit file needs to be set to 644 :
$ sudo chmod 644 /lib/systemd/system/pyrpc.service

Step 3 – Configure systemd

Now the unit file has been defined you can tell systemd to start it during the boot sequence.
Reload the systemd
$ sudo systemctl daemon-reload
Enable your service on system boots
$ sudo systemctl enable pyrpc.service
(performs symlink - ln -s '/usr/lib/systemd/system/pyrpc.service ' '/etc/systemd/system/multi-user.target.wants/pyrpc.service ')

Step 4 – Check status of your service

System reboot
$ sudo reboot
You can check the status of your service using :
$ sudo systemctl status pyrpc.service

Leave a Reply

Your email address will not be published. Required fields are marked *