Systemctl – running tasks at boot on a Raspberry Pi

There are numerous ways to run programs or scripts at boot on Debian like *nix systems like the Raspberry Pi’s Raspbian. They include :

  • rc.local – located at /etc/rc.local, a file that loads and executes commands at boot
  • autostart desktop files – for when Raspbian boots into the Pixel Desktop
  • systemd – these run as services when the pi boots regardless of console or desktop environment
  • crontab – the chronological scheduler for *nix

All of these options have their advantages and disadvantages

rc.local
Advantages – Very simple to add commands to this file to execute at boot
Disadvantages – Depending on dependencies loaded when rc.local loads, some commands can fail to execute succesfully

autostart
Advantages – Makes customising the Raspberry Pi desktop experience flexible easily
Disadvantages – Only of use if you are using the desktop, and is “an unstoppable force”

systemd
Advantages – Can be run at boot, but also stopped and then restarted after boot – very controllable
Disadvantages – Seems daunting for many users, so they just keep using rc.local

crontab
Advantages – Not only can it run code at boot, but also at scheduled intervals
Disadvantages – You are locked into a schedule and cannot manually intervene to stop and restart whats running

We are going to look at systemd, .service files and the systemctl command.

How it works

At boot, all enabled services in systemd run. To get them to run, they have to have an enabled and have a valid .service file in a place systemd can run it. The systemctl command can start, stop, restart, enable, disable and refresh these services live in the console.

What a .service file looks like and what it does

Below is an example of a .service file , which is located in /etc/systemd/system and is called temp.service, therefore its full path is /etc/systemd/system/temp.service , and is created with sudo nano /etc/systemd/system/temp.service

[Unit]
Description=stats3
After=network.target

[Service]
ExecStart=/usr/bin/python -u temp.py
WorkingDirectory=/home/pi/Scripts
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

We can break down in lay persons terms what this is acheiving :

The Description – in this case stats3 is what the service will have in the description field when you run a command such as systemctl --type=service
After=network.target – make sure this service runs after the network is up – often very important if your service requires access to the internet or local network
ExecStart – the actual command you want to run
WorkingDirectory – the directory in which the file we want to execute lives
User – since we offered /usr/bin/python -u temp.py in the example above, , the -u switch dictates the user running the command , and this can be configured to be any user – even though you have booted and logged in as pi for example, the service could be run as perhaps user mpd

There are a few other lines in there, but the main editable needs are above to get someone started.

So the mainstay of this is that the above file will basically run, once enabled, the temp.py python script at boot after the network is up

So we’ve created the file temp.service, but how do we make it work so that it executes the temp.py script at boot ? Easy :

sudo systemctl enable temp

Notice we dont include the .service file extension. This would start the service after the next reboot. How do I start it now ? Easy :

sudo systemct start temp

And to stop it :

sudo systemctl stop temp

And what if I just want to restart the service instead of starting and stopping it because I’ve edited temp.py ?

sudo systemctl restart temp

Sometimes doing a systemctl restart will complain that you need to sudo systemctl daemon-reload due to the service having dependencies that systemd thinks will be negatively impacted by a basic restart – so you will need to run sudo systemctl daemon-reload before sudo systemctl restart *service name*

You can deduce from this that to disable the service at boot, the command would simply be :

sudo systemctl disable temp

Hopefully the above will give enough of an insight for you to begin making your own systemd .service files and automating your boot up process elegantly. No more rc.local !

Posted in Raspberry Pi and tagged , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *