Question – How to autorun a Python script using systemd. How to create own systemd service using Python script. How to configure Python script to start as systemd. How to manage Python service with systemctl?
Use this tutorial to run your Python script as system service under systemd. You can easily start, stop or restart your script using systemctl command. This will also enable to autorun Python script on system startup.
Step 1 – Dummy Python Application
First of all, I have used a dummy Python script which listens on a specified port. Edit a Python file as following
sudo vi /usr/bin/dummy_service.py
and add following content for dummy, You can use your own Python script as per requirements.
1 2 3 4 5 6 7 8 9 10 11 12 | #!/usr/bin/python3 import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("localhost", 9988)) s.listen(1) while True: conn, addr = s.accept() data = conn.recv(1024) conn.close() my_function_that_handles_data(data) |
Step 2 – Create Service File
Now, create a service file for the systemd as following. The file must have .service extension under /lib/systemd/system/ directory
sudo vi /lib/systemd/system/dummy.service
and add the following content in it. Change Python script filename ad location. Also update the Description.
[Unit] Description=Dummy Service After=multi-user.target [email protected] [Service] Type=simple ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py StandardInput=tty-force [Install] WantedBy=multi-user.target
Step 3 – Enable Newly Added Service
Your system service has been added to your service. Let’s reload the systemctl daemon to read new file. You need to reload this deamon each time after making any changes in in .service file.
sudo systemctl daemon-reload
Now enable the service to start on system boot, also start the service using the following commands.
sudo systemctl enable dummy.service sudo systemctl start dummy.service
Step 4 – Start/Start/Status new Service
Finally check the status of your service as following command.
sudo systemctl status dummy.service
Use below commands to stop, start and restart your service manual.
sudo systemctl stop dummy.service #To stop running service sudo systemctl start dummy.service #To start running service sudo systemctl restart dummy.service #To restart running service
11 Comments
it helped me on centos 8.
i have added “WorkingDirectory” to solve some issues.
thanks for this post.
A fine and simple to follow tutorial. I would add a couple of caveats – with the .service file as written the python script will be run as root and this may have unintended consequences. Also the environment will be different from that for a normal user. To fix add the lines User=username and Group=groupname before the ExecStart line. To add environment variables expected by the script add the line Environment=”variable_name=variable_value” before the ExecStart line.
Thank you very much.
Could you kindly also provide where I can find more information concerning this?
for example the type of service and other parameters it uses such as confilicts, etc?
I’d greatly appreciate it
Hi,
I am trying to deploy my flask code as service.
it uses a virtual environment that has flask installed. How can I link my virtual environment with my python code?
For anyone still looking for an answer:
Add the path of the virtual environment’s python binary.
Example, if your virtual env is installed at home/user/environ, then change the following
ExecStart=/usr/bin/python3 /usr/bin/dummy_service.py
To:
ExecStart=/home/user/environ/bin/python3 /usr/bin/dummy_service.py
Thanks – perfect for my needs.
Hi, how it will work if I would add time.sleep(23) in my python script? Will run every 23 minutes ?
sleep(23) would sleep for 23 seconds. But, yes, that would work in a service script.
Hello, this is a good tutorial. Is there more information on capturing logs? I am familiar with the logging module for python but incase there were mistakes made for catching exeptions etc., how could these be written to a file?
THANKS!
thanks bro