Search
 
SCRIPT & CODE EXAMPLE
 

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

python service linux

If you want To use python in Linux You can go With Wing Personal Editor cause I am also a Linux user and it is free and fast also
Comment

PREVIOUS NEXT
Code Example
Python :: run matlab code in python 
Python :: python pandas column where 
Python :: python isinstance list 
Python :: mkvirtualenv environment python 3 
Python :: pandas remove leading trailing spaces in dataframe 
Python :: print flush python 
Python :: django in conda 
Python :: excute a command using py in cmd 
Python :: change marker border color plotly 
Python :: how to print a column from csv file in python 
Python :: python int to binary 
Python :: python dict setdefault 
Python :: raspberry pi keyboard python input 
Python :: create columns in streamlit 
Python :: python split string size 
Python :: python invert an array 
Python :: python tkinter projects 
Python :: python check if there is internet connection 
Python :: django view - APIView (retrieve, update or delete - GET, PUT, DELETE) 
Python :: xlabel and ylabel in python 
Python :: how print 2 decimal in python 
Python :: turn python script into exe 
Python :: python append value to dictionary list 
Python :: dataframe to ftp 
Python :: python raise typeerror 
Python :: Sending POST request in Django 
Python :: python string format 
Python :: match statement 
Python :: pandas xa0 
Python :: get the time of 1 minute later in python 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =