If you have created a Qt application - or any other application - for the Raspberry Pi 4, you often want the application to be called immediately after restarting the Raspberry after the application has been completed.
This is often attempted with start scripts that can be entered in various places.
However, it is more reasonable to set this up via systemd . I used a raspbian-buster-lite image and a Qt installation as described in Qt on the Raspberry Pi 4 as a starting point.
The Qt application is located in the directory "/home/pi/application" and is named "application_one" in this example.

Creating a .service file

The first thing to do is to create a .service file in the "/etc/systemd/system" directory:

sudo nano application_one.service

The following is now entered here:

[Unit]
Description=Qt application autostart
After=graphical.target
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/application
ExecStart=/home/pi/application/application_one

[Install]
WantedBy=multi-user.target

The entries are actually self-explanatory. The application "application_one" (ExecStart=/home/pi/application/application_one) is started with the user account "pi" (User=pi). The entry "After=network-online.target" still states that the application is started until the network connection is established.

</:code2:></:code1:>

Make the service known to the system

Then you have to tell the system that the new service should be active:

sudo systemctl enable application_one.service

Then perform a reboot and the application should start automatically.

</:code3:>

Application does not start?

If the application does not start automatically, you can log in with

sudo systemctl status application_one.service

display the status and use the information to troubleshoot. </:code4:>

Walter Prechtl

Walter Prechtl

Updated at: 08. March 2024
Reading time: 2 minutes