How to use systemd for applications start

On the latest Debian distributions is installed by default systemd instead of sysvinit as initialization system. This article illustrates pratically how to use it.

The original version of this article is available on:

Create a service

To launch a script at startup a proper service file have to be created and added to the systemd.

Let's create for example a service file to launch a Python program called Create as root a file called blink.service in /lib/systemd/system directory.

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

then save inside it this content:

Description=Blinking led

ExecStart=/usr/bin/python /root/


Next systemd has to be configured, enabling the service so at the next reboot it will be started automatically. First off, though, the systemd itself has to be reloaded in order to detect the new service definition:

sudo systemctl daemon-reload

Next, enable the service

sudo systemctl enable blink

Check if it is enabled:

systemctl list-unit-files | grep enabled

Of course, it can be started manually just now:

sudo systemctl start blink

The PID (process id) can be now checked:

ps -ef | grep blink
pi         817     1  0 09:19 ?        00:00:00 /usr/bin/python

it is 817 (the number will be different in your case). Please note that it has been started from systemd process, in fact the PPID (parent process id) is 1.

Test what happens if the program crashes and/or terminates.

In order to simulate a crash, we kill it manually:

kill 817

Now we can check it was restarted looking again to the PID (process id):

ps -ef | grep blink
pi        1037     1  0 09:19 ?        00:00:00 /usr/bin/python

it is now 1037 (PPID always 1), so, as it is different, that means has been restarted automatically after the kill.

Use the systemd logging system

To check what is doing your service launch this command:

journalctl -f blink

It is possible, of course, to add our own messages to this log. In Python, for example, install the python-systemd package:

apt-get update
apt-get install python-systemd

add these lines to your code:

import logging
from systemd.journal import JournalHandler

log = logging.getLogger('blink')
log.setLevel(logging.INFO)"Blinking led started")

Open another terminal and launc journalctl with follow option (-f):

journalctl -u -f blink
May 13 11:49:24 roadrunner systemd[1]: Started Blinking led.
May 13 11:49:25 roadrunner /root/[18081]: Blinking led started

Create a timer for your service

It is possible to use the systemd timers to launch a service instead of using cron

Example 1: Run a service every hour

Create a systemd service file in /lib/systemd/system/blink.service with this contents:

Description=Blinking led


Create a systemd timer file for this service in /lib/systemd/system/blink.timer with this contents::

Description=Runs every hour

# Time to wait after booting before we run first time
# Time between running each consecutive time


Reload the service definitions:

sudo systemctl daemon-reload

Then is your are using the same file of previous example disable the blink.service and enable just the blink.timer to start it after the bootstrap:

sudo systemctl disable blink.service
sudo systemctl enable blink.timer

Let's try it now:

sudo systemctl start blink.timer

Example 2: Run a service daily

Description=Runs daily at midnight

OnCalendar=*-*-* 00:00:00


Changing the OnCalendar parameter is possible to start the blink.service in a lot of different way:

Minimal form                   Normalized form
Sat,Thu,Mon-Wed,Sat-Sun    ==> Mon-Thu,Sat,Sun *-*-* 00:00:00
Mon,Sun 12-*-* 2,1:23      ==> Mon,Sun 2012-*-* 01,02:23:00
Wed *-1                    ==> Wed *-*-01 00:00:00
Wed-Wed,Wed *-1            ==> Wed *-*-01 00:00:00
Wed, 17:48                 ==> Wed *-*-* 17:48:00
Wed-Sat,Tue 12-10-15 1:2:3 ==> Tue-Sat 2012-10-15 01:02:03
*-*-7 0:0:0                ==> *-*-07 00:00:00
10-15                      ==> *-10-15 00:00:00
monday *-12-* 17:00        ==> Mon *-12-* 17:00:00
Mon,Fri *-*-3,1,2 *:30:45  ==> Mon,Fri *-*-01,02,03 *:30:45
12,14,13,12:20,10,30       ==> *-*-* 12,13,14:10,20,30:00
mon,fri *-1/2-1,3 *:30:45  ==> Mon,Fri *-01/2-01,03 *:30:45
03-05 08:05:40             ==> *-03-05 08:05:40
08:05:40                   ==> *-*-* 08:05:40
05:40                      ==> *-*-* 05:40:00
Sat,Sun 12-05 08:05:40     ==> Sat,Sun *-12-05 08:05:40
Sat,Sun 08:05:40           ==> Sat,Sun *-*-* 08:05:40
2003-03-05 05:40           ==> 2003-03-05 05:40:00
2003-03-05                 ==> 2003-03-05 00:00:00
03-05                      ==> *-03-05 00:00:00
hourly                     ==> *-*-* *:00:00
daily                      ==> *-*-* 00:00:00
monthly                    ==> *-*-01 00:00:00
weekly                     ==> Mon *-*-* 00:00:00
*:20/15                    ==> *-*-* *:20/15:00

Check the list of timers

systemctl list-timers --all
NEXT                          LEFT     LAST                          PASSED       UNIT                         ACTIVATES
Mon 2018-05-14 00:00:00 CEST  10h left Sun 2018-05-13 13:14:44 CEST  2min 17s ago blink.timer                  blink.service
Mon 2018-05-14 10:38:28 CEST  21h left Sun 2018-05-13 10:38:28 CEST  2h 38min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
n/a                           n/a      n/a                           n/a          apt-daily-upgrade.timer      apt-daily-upgrade.service
n/a                           n/a      n/a                           n/a          apt-daily.timer              apt-daily.service


How to start a script right before shutdown

This is an example on how to start a Python script called at shutdown:

Description=Send a message on the Oled at shutdown



Save is as /lib/systemd/system/oled.service

Reload the new service definition:

systemctl daemon-reload

Enable the new service:

systemctl enable oled.service

Then shutdown:


How to start a script into the text console


ExecStart=/usr/bin/python3 /home/pi/