Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Tutorials»How To Setup Autorun a Python Script Using Systemd

    How To Setup Autorun a Python Script Using Systemd

    RahulBy RahulSeptember 28, 20172 Mins ReadUpdated: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.

    Python
    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 
    
    python script service systemctl systemd
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Adobe Flash Player 32 on Fedora 34/33
    Next Article How to Install MariaDB 10.4 on CentOS/RHEL 7/6

    Related Posts

    (Resolved) userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms

    Updated:May 10, 20221 Min Read

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How To Enable SSH Server on Ubuntu 22.04

    Updated:April 22, 20222 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    How to Start & Stop Windows Service via Command Line

    Updated:March 29, 20222 Mins Read

    10 Best Linux Video Players in 2022

    Updated:February 18, 20226 Mins Read

    11 Comments

    1. Arie on September 22, 2020 11:59 am

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

      thanks for this post.

      Reply
    2. MArtin on April 30, 2020 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.

      Reply
    3. Hossein on March 10, 2020 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

      Reply
    4. Wasim Akram on December 3, 2019 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?

      Reply
      • Prashant on June 2, 2021 5:24 am

        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

        Reply
    5. Dan on September 5, 2019 2:42 pm

      Thanks – perfect for my needs.

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

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

      Reply
      • DanH on December 8, 2019 2:30 am

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

        Reply
    7. K on June 26, 2019 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?

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

      THANKS!

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

      thanks bro

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.