banner



How To Create A Service In Python

Setup a python script as a service through systemctl/systemd

WasiUllah Khan

There are several ways you can run your program as a background service in Linux such as crontab, .bashrc, etc but today I'll write about systemd. I was initially looking for a way to run my python script as a background service so even if the server restarts for some reason, my script will run in the background regardless and I found that systemd allows me to do that. Let's get started

I'll be setting this up on an Ubuntu 18.10 machine.

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' 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.

How To Create A Service In Python

Source: https://medium.com/codex/setup-a-python-script-as-a-service-through-systemctl-systemd-f0cc55a42267

Posted by: owenobinew2000.blogspot.com

0 Response to "How To Create A Service In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel