From Python to Daemon: How to Turn Your Python App into a Linux Service Controlled by Systemd

Ebo Jackson
Level Up Coding
Published in
5 min readJul 25, 2023

--

Running a Python application as a Linux service controlled by Systemd is an excellent way to ensure that your application starts automatically when the machine boots up and is managed efficiently. Systemd is the default init system for many modern Linux distributions and provides a robust and standardized way to manage services.

In this article, we will guide you through the process of turning your Python app into a Systemd service step-by-step.

Prerequisites

Before we begin, make sure you have the following:

  1. A Linux machine with Systemd support (Systemd is available on most modern Linux distributions).
  2. A Python application that you want to run as a service.

Step 1: Create a Service Script

The first step is to create a script that will be executed by Systemd to start and manage your Python application. This script will be a bash script, and it should handle starting, stopping, and restarting your Python app.

Create a new file, let’s call it my_python_app.sh, and place the following content inside it:

#!/bin/bash

# Replace 'path_to_your_python_app' with the actual path to your Python application script.
APP_PATH="/path/to/your/python/app.py"
PID_FILE="/var/run/my_python_app.pid"

start() {
if [ -f $PID_FILE ]; then
echo "The service is already running."
else
nohup python3 $APP_PATH &> /dev/null &
echo $! > $PID_FILE
echo "Service started."
fi
}

stop() {
if [ -f $PID_FILE ]; then
kill $(cat $PID_FILE)
rm $PID_FILE
echo "Service stopped."
else
echo "The service is not running."
fi
}

restart() {
stop
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac

Make the script executable by running:

chmod +x my_python_app.sh

Step 2: Test the Script

Before creating the Systemd service, let’s test the script to ensure it starts and stops your Python application correctly. Open a terminal and navigate to the directory where my_python_app.sh is located. Then, run the following commands:

To start the service:

./my_python_app.sh start

To stop the service:

./my_python_app.sh stop

To restart the service:

./my_python_app.sh restart

Ensure that your Python app behaves as expected when started and stopped using the script.

Step 3: Create the Systemd Service File

Now that your service script is working correctly, it’s time to create a Systemd service unit file. This file tells Systemd how to manage your Python application.

Open a new file using a text editor with administrative privileges, such as nano or vim. Let's call the file my_python_app.service. Add the following content to the file:

[Unit]
Description=My Python App
After=network.target

[Service]
Type=simple
User=your_username
WorkingDirectory=/path/to/your/python/app/directory
ExecStart=/path/to/your/my_python_app.sh start
ExecStop=/path/to/your/my_python_app.sh stop
ExecReload=/path/to/your/my_python_app.sh restart
Restart=always

[Install]
WantedBy=multi-user.target

Replace the placeholders (your_username, /path/to/your/python/app/directory, and /path/to/your/my_python_app.sh) with appropriate values.

  • Description: A brief description of your service.
  • After: Specifies that the service should start after the network is available.
  • Type: Indicates the process type. Set it to simple for straightforward long-running processes.
  • User: The user under which the service will run. Use an appropriate user with the necessary permissions.
  • WorkingDirectory: The directory where your Python app is located. Make sure to provide the correct path.
  • ExecStart, ExecStop, and ExecReload: The paths to your service script and the corresponding actions (start, stop, and restart).
  • Restart: Specifies when the service should be restarted. In this case, we set it to always to restart the service automatically if it crashes.
  • WantedBy: Specifies which target should include this service. multi-user.target ensures that the service is started during the boot process.

Step 4: Enable and Start the Service

To enable your service and start it immediately, run the following commands:

sudo cp my_python_app.service /etc/systemd/system/
sudo systemctl enable my_python_app.service
sudo systemctl start my_python_app.service

You have now successfully turned your Python application into a Linux service controlled by Systemd. The service will start automatically when the machine boots up, and Systemd will take care of managing it.

Using Systemd to manage your Python application as a Linux service provides an efficient and standardized way to ensure your app starts on boot and is resilient to failures. By following the steps outlined in this article, you can easily create a Systemd service unit file for your Python app and integrate it seamlessly into your Linux system. Enjoy the benefits of having your Python app automatically run in the background, allowing you to focus on other important tasks with ease.

Alternatively:

It is possible to turn your Python app into a Linux service controlled by Systemd without using a shell script as an intermediary. Instead, you can directly create a Systemd service unit file that runs your Python application. This method eliminates the need for a separate shell script and simplifies the setup process.

Here’s how you can do it:

Step 1: Create the Systemd Service Unit File

Open a new file using a text editor with administrative privileges, such as nano or vim. Let's call the file my_python_app.service. Add the following content to the file:

[Unit]
Description=My Python App
After=network.target

[Service]
Type=simple
User=your_username
WorkingDirectory=/path/to/your/python/app/directory
ExecStart=/usr/bin/python3 /path/to/your/python/app.py
Restart=always

[Install]
WantedBy=multi-user.target

Replace the placeholders (your_username, /path/to/your/python/app/directory, and /path/to/your/python/app.py) with appropriate values.

  • Description: A brief description of your service.
  • After: Specifies that the service should start after the network is available.
  • Type: Indicates the process type. Set it to simple for straightforward long-running processes.
  • User: The user under which the service will run. Use an appropriate user with the necessary permissions.
  • WorkingDirectory: The directory where your Python app is located. Make sure to provide the correct path.
  • ExecStart: The command to start your Python application directly without using a shell script.
  • Restart: Specifies when the service should be restarted. In this case, we set it to always to restart the service automatically if it crashes.
  • WantedBy: Specifies which target should include this service. multi-user.target ensures that the service is started during the boot process.

Step 2: Enable and Start the Service

To enable your service and start it immediately, run the following commands:

sudo cp my_python_app.service /etc/systemd/system/
sudo systemctl enable my_python_app.service
sudo systemctl start my_python_app.service

Your Python application is now set up as a Linux service controlled by Systemd. It will start automatically when the machine boots up, and Systemd will handle its management without the need for an intermediary shell script.

Conclusion

By directly creating a Systemd service unit file for your Python application, you can streamline the setup process and eliminate the need for an additional shell script. This approach simplifies the configuration and ensures that your Python app runs as a Linux service efficiently. Enjoy the benefits of automatic startup and seamless management with Systemd.

Level Up Coding

Thanks for being a part of our community! Before you go:

🔔 Follow us: Twitter | LinkedIn | Newsletter

🚀👉 Join the Level Up talent collective and find an amazing job

--

--