How to set up the QT (UI) program to start automatically at boot in the Linux operating system and grant it root privileges

Hello everyone,
I have encountered a problem with a Qt program, so I am posting the issue and hope that fellow software developers can provide assistance.

problem description
I have a QT program with an UI interface, which is used for automatic startup upon boot and communicates with the serial port device ttyTHS1. To solve this problem, two points need to be achieved:
1. Implement the UI program to start automatically upon boot;
2.The service program requires root privileges.

Implementation Plan:
Currently, I have two options available for selection:
1、 Write a desktop file
The content of the desktop file is as follows:

[Desktop Entry]
Version=1.0
Name=pm_client
Exec=sudo /home/rk/power_management_client
StartupNotify=false
NoDisplay=true
Type=Application
 
Categories=System;Utility;Archiving;

Execute the command "sudo visudo" and append the following content to the last line:
rk ALL=(ALL) NOPASSWD: /home/rk/power_management_client
2、Write a service
The content of the service file is as follows:

[Unit]
Description=Qt GUI demo via systemd
After=graphical.target
Wants=graphical.target

[Service]
Type=simple
Environment="DISPLAY=:1"
Environment="XAUTHORITY=/run/user/1000/gdm/Xauthority"
ExecStartPre=/bin/sleep 3
ExecStart=/opt/rk/power_management_client
Restart=on-failure

[Install]
WantedBy=graphical.target

Both of these two approaches can achieve the goal at present. However, I would like to inquire if there is a more reasonable solution?

1 Like

The service is the ideal way for me I think. Does it run in the background or actively on the screen? Will it need to wait for a login if that is the case?

If it needs to be run under a logged in gnome session in this case you could do a user systemd service firing sudo /opt/rk/power_management_client and give that script passwordless access for only your user. Then it will start on login every time.

Hi jmgibson81,
Thank you for your reply.
However, I have found a problem when using the service: When the system enters the login interface, the service starts running. After entering the password, a LOGO appears on my system. At this time, the UI interface will cover the LOGO.
So in summary, I need the UI interface to start after entering the desktop system. Therefore, using the service still has some flaws.

Okay. In this case my path would be to create a .desktop file that runs that script via the aforementioned passwordless sudo for just that script.

[Desktop Entry]
Type=Application
Name=Your Program
Comment=What it does
Exec=/usr/bin/sudo /path/to/script
Icon=/path/to/icon
Categories=System;
Terminal=false

Add this to your ~/.local/share/applications folder and it should show up as an option to add to your startup applications. And this method will also allow you to manually start it as you would any other application in the GUI interface.

1 Like

If you do not want to rip open a security hole by running a GUI app as root in your session, the proper way would indeed be to split the app in two halves and have a service running as root that provides info through dbus to the GUI that runs as a user… ( And then run the root part as systemd service and the GUI side as autostart desktop app)

5 Likes

Ogra’s approach is better. Looking at that, I failed at good ideas. Follow his approach, not mine.

2 Likes

Thank you both for your answers. I will test both of your suggestions.

1 Like