• Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 20.04
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How To Setup Autorun a Python Script Using Systemd

Written by Rahul, Updated on October 8, 2018

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 

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

10 Comments

  1. Avatar Arie Reply
    September 22, 2020 at 11:59 am

    it helped me on centos 8.
    i have added “WorkingDirectory” to solve some issues.

    thanks for this post.

  2. Avatar MArtin Reply
    April 30, 2020 at 11:23 am

    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.

  3. Avatar Hossein Reply
    March 10, 2020 at 7:04 am

    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

  4. Avatar Wasim Akram Reply
    December 3, 2019 at 9:22 pm

    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?

  5. Avatar Dan Reply
    September 5, 2019 at 2:42 pm

    Thanks – perfect for my needs.

  6. Avatar Olaf Reply
    July 24, 2019 at 10:48 pm

    Hi, how it will work if I would add time.sleep(23) in my python script? Will run every 23 minutes ?

    • Avatar DanH Reply
      December 8, 2019 at 2:30 am

      sleep(23) would sleep for 23 seconds. But, yes, that would work in a service script.

  7. Avatar K Reply
    June 26, 2019 at 2:43 pm

    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?

  8. Avatar khmel Reply
    March 31, 2019 at 4:32 pm

    THANKS!

  9. Avatar mustafa Reply
    February 27, 2019 at 3:26 pm

    thanks bro

Leave a Reply Cancel reply

Popular Posts

  • How to View or List Cron Jobs in Linux
  • How to Install PHP 8 on Ubuntu 20.04
  • How to Set Up SSH Tunnel with PuTTY
  • How to Install MySQL 8.0 on Ubuntu 20.04
  • Issue with phpMyAdmin and PHP: Warning in ./libraries/sql.lib.php#613 count(): Parameter must be an array or an object that implements Countable”
  • How to Install Tor Browser on Ubuntu 20.04
  • How to Allow Remote Connections to MySQL
  • How To Install VNC Server on Debian 10
© 2013-2021 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy