Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python script as service linux

#Almost all versions of Linux come with systemd out of the box, but if your’s didn’t come with it then you can simply run the following command:

sudo apt-get install -y systemd

#Note: The -y flag means to install the packages and dependencies quickly.

To check which version of systemd you have simply run the command:

systemd --version

#Create a python file whatever you like. I’m going to call mine test.py.

sudo nano test.py

import time
from datetime import datetime
while True:
    with open("timestamp.txt", "a") as f:
        f.write("The current timestamp is: " + str(datetime.now()))
        f.close()
    time.sleep(10)
    
#The above script will write the current timestamp in the file after every 10 seconds. Let’s write the service now.

sudo nano /etc/systemd/system/test.service (name of the service which is test in this case)

[Unit]
Description=My test service
After=multi-user.target
[Service]
Type=simple
Restart=always
ExecStart=/usr/bin/python3 /home/<username>/test.py
[Install]
WantedBy=multi-user.target

#Insert the username in your OS where <username> is written. The ExecStart flag takes in the command that you want to run. So basically the first argument is the python path (in my case it’s python3) and the second argument is the path to the script that needs to be executed. Restart flag is set to always because I want to restart my service if the server gets restarted. For more information on this, you can go to this link. Now we need to reload the daemon.

sudo systemctl daemon-reload

#Let’s enable our service so that it doesn’t get disabled if the server restarts.

sudo systemctl enable test.service

#And now let’s start our service.

sudo systemctl start test.service

#Now our service is up and running.

Note: The file will be written in the root directory (/) because the program will write in the path from the perspective of systemd. To change that simply edit out the file path. For example:

import time
from datetime import datetime
path_to_file = "enter the desired path of the file"
while True:
    with open(path_to_file, "a") as f:
        f.write("The current timestamp is: " + str(datetime.now()))
        f.close()
    time.sleep(10)
    
    
There are several commands you can do to start, stop, restart, and check status.

To stop the service:

sudo systemctl stop name_of_your_service

To restart:

sudo systemctl restart name_of_your_service

To check status:

sudo systemctl status name_of_your_service

#This was a very basic introduction to systemd aimed at beginners who want to get started with writing their own systemd services for python. 
#If you want a deep dive into systemd and systemctl, here is a detailed guide by the digital ocean.

NOTE: This doesn’t only apply to python scripts. You can basically run any program with this regardless of the programming language your program is written in.
 
PREVIOUS NEXT
Tagged: #python #script #service #linux
ADD COMMENT
Topic
Name
6+6 =