Codementor Events

How to run a Python script in Linux with SYSTEMD

Published Nov 08, 2021Last updated May 06, 2022

Running a python script is easy, right?

python app.py

That's it! We are done here. Have a nice day 😃

Wait a second!
When you want that script to run automatically on startup, things get changed. You have some other options to accomplish that like crontab, rc.local etc. But for this blog, I am focusing on SYSTEMD.
DISCLAIMER: I am not experienced linux developer or user. This post is actually note to myself, because I forget very quickly 😃

What is SYSTEMD

You can read from Wikipedia
I said it is gonna be short note for myself -_-

How SYSTEMD helps us to automatically run python scripts.

DISCLAIMER AGAIN: I only tried this method on debian based linux systems like ubuntu and raspbian.
Not only python scripts, but also anything you want.
1. Creating a unit file
First of all, you should create a service unit configuration file

sudo nano /lib/systemd/system/myservice.service

2. Describe the service with basics
This is the basic working examples of service description. You can google every line of it.

  1. Running python directly
    I think this is the simplest way to run a python script but not recommended for most case.
    Be sure about python path and don't forget to give full path to the python project.
[Unit]
Description=My Lovely Service
After=network.target

[Service]
Type=idle
Restart=on-failure
User=root
ExecStart=/usr/bin/python /path-to-your-python-project/python_file.py

[Install]
WantedBy=multi-user.target
  1. Running python within folder
    It would be better to go to relevant folder and run python scripts at there. It relaxes your mind about mismatch folder paths.
    We are now using bash for running multiple scripts.
[Unit]
Description=My Lovely Service
After=network.target

[Service]
Type=idle
Restart=on-failure
User=root
ExecStart=/bin/bash -c 'cd /home/ubuntu/project/ && python app.py'

[Install]
WantedBy=multi-user.target
  1. Running with python virtual environment
    After that, you can add as many commands as you want 😃
[Unit]
Description=My Lovely Service
After=network.target

[Service]
Type=idle
Restart=on-failure
User=root
ExecStart=/bin/bash -c 'cd /home/ubuntu/project/ && source env/bin/activate && python app.py'

[Install]
WantedBy=multi-user.target

3. Set permission for the unit file

sudo chmod 644 /lib/systemd/system/myservice.service

You can play with chmod at the link.
4. Configure systemd
We need to run daemon-reload and then enable the service.

sudo systemctl daemon-reload
sudo systemctl enable myservice.service

Extra commands for maintaining systemd

# start a service
sudo systemctl start application.service
sudo systemctl start application

# stop a service
sudo systemctl stop application.service

# restart a service
sudo systemctl restart application.service

# reload a service
sudo systemctl reload application.service

# enable a service
sudo systemctl enable application.service

# disable a service
sudo systemctl disable application.service

# get the status log of a service
systemctl status application.service

Take care yourself. I will add detailed workarounds about systemd later.

Discover and read more posts from Ufuk Şafak
get started
post comments2Replies
RAYMOND GRACE
3 years ago

Raygrace ray@wpid.ca 7809947423 cell Re python script I want to automate processing the payment of shopping cart balance using the cart UserID and password. Could you automate it to run on a windows machine or in a Windows Partition on my linux machine?

RAYMOND GRACE
3 years ago

Can you open a windows terminal and then run the script In the terminal from the Windows machine?