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 :: calculate mse loss python 
Python :: del mutiple indexes at once 
Python :: get forex exchange rates in python 
Python :: Program to illustrate the use of nested if statement Average in python Grade =80 and above A =70 and <80 B =60 and <70 C =50 and <60 D Otherwise 
Python :: saving specific column with pd 
Python :: NumPy left_shift Code When inputs and bit shift are numbers 
Python :: All possible combinations of multiple columns 
Python :: tikzplotlib set figure 
Python :: qt list widget let editable 
Python :: How to run a method before/after all class function calls with arguments passed? 
Python :: adjoint of 3x3 matrix in numpy 
Python :: tkintre sub windows 
Python :: celery 5.2.3 decorators 
Python :: knn compute_distances_two_loop 
Python :: XML to table form in Excel 
Python :: print(i) 
Python :: ternary operator in list comprehension python 
Python :: Fetch all links from a given webpage 
Python :: How to setup Conda environment and package access extension from within Jupyter 
Python :: Python beginner question - trying to understand return statement 
Python :: tdlib python 
Python :: xchacha20 
Python :: ring raise an exception 
Python :: how to add log to a variable in plotly 
Python :: python adx indicator 
Python :: webdriver antibot 
Python :: We want to estimate the cost of painting a property. Interior wall painting cost is Rs.18 per sq.ft. and exterior wall painting cost is Rs.12 per sq.ft. 
Python :: How to check whether a nested hash element exists in python 
Python :: legend outside subplot not displayed 
Python :: "json" is not defined 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =