Using Systemd to Start a Python Application with Virtualenv
Systemd is great for starting and stopping services under Linux. When you want to start a Python application that uses virtualenv, you need to take special care. This article explains how to create a systemd script using the example of the open source Timetagger Python application.
On Linux systems, systemd is an init system used to bootstrap userspace and manage user processes. At the time of its introduction it was controversial, but now is used by the majority of all modern Linux distributions.
So when you want to automatically run an application at every start of the Linux operating system, you need to create a configuration file. I show later how to do that.
When installing Python applications that have library dependencies, it is very useful to
use the service of virtualenv. With the help of virtualenv you can isolate Python applications so
that the library dependencies do not interfere with each other.
This is needed if you have more Python applications installed at the same time in your system.
Since Python 3.3, a subset of virtualenv is integrated under the
Timetagger is an open source Python time tracker application developed by Almar Klein. I use this as an example here because I find Timetagger useful in my work. It features a comfortable web interface to track your time and also has an API. Using the Timetagger CLI you can start and stop time tracking on the command line. This is especially handy for software developers.
When you host the application yourself you probably want to run it automatically at every start of your system. That is where the systemd script comes into play.
You need to install Timetagger with virtualenv in order to use the systemd script.
After you have cloned Timetagger from GitHub you can run the following commands to install it using virtualenv:
cd timetagger python3 -m venv venv . venv/bin/activate pip3 install -r requirements.txt
Now all the dependencies are installed using virtualenv.
You need to create the systemd service script (timetagger.service) in the system directory using a text editor:
[Unit] Description=Timetagger service [Service] User=Your Linux user name WorkingDirectory=Your timetagger folder ExecStart=Your timetagger folder/venv/bin/python3 -m timetagger & [Install] WantedBy=multi-user.target
Your timetagger folder with the actual Timetagger directory name on you system.
I use the
& at the end of the command to make Timetagger run in the background.
The key to running a Python application that has been installed via virtualenv is to
execute Python via the
venv/bin folder. Then the correct set of libraries are used.
Note that you need admin rights to create the timetagger.service file.
You can now start timetagger with:
sudo systemctl start timetagger
In order to run timetagger automatically at every system start enable it like so:
sudo systemctl enable timetagger
When you have started Timetagger in the manner described above, you can stop it
by sending it a
kill -SIGSTOP pid
You can get the pid (process id) via the
ps ax command. Look for the Timetagger name.
Running Python applications that use virtualenv automatically in systemd is not rocket science. I hope with this article I have shown you how to do it.
Cover image by OpenAI Dall-E2: “Yellow snake with a time tagger.”
This work is licensed under a Creative Commons Attribution 4.0 International License.