If you have created a Qt application – or any other application – for the Raspberry Pi 4, you will very often want to have the application called up immediately after the Raspberry is restarted, once you have completed the application. This is often attempted using start scripts, which can be entered in various places. However, it is more sensible to set this up via systemd. As a starting point, I used a raspbian-buster-lite image and a Qt installation as described in Qt on the Raspberry Pi 4. The Qt application is located in the "/home/pi/application" directory and, in this example, is named "application_one".
Creating the .service file
First of all, a .service file must be created in the directory "/etc/systemd/system":
sudo nano application_one.service
The following is now entered:
[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 launched using the user account "pi" (User=pi). The entry "After=network-online.target" also means that the application is only started once the network connection has been established.
Making the service known to the system
The system then needs to be informed that the new service should be active:
sudo systemctl enable application_one.service
Then perform a restart, and the application should launch automatically.
Application is not launching?
If the application does not launch automatically, you can display the status using
sudo systemctl status application_one.service
and use the information to start troubleshooting.