I'm facing the same problem than you: My python GTK app needs to run commands as root, but using pkexec
makes it asking each time for the root password, which is really annoying.
I see two ways to solve that "issue":
1. Run your app with pkexec
That solution would fit more for your case.
Write a shell script which runs your python app with pkexec
. With that solution, the user is asked only once, and your app run as root, therefore it doesn't require to use pkexec
anymore, and can run any commands.
The downside of this is that you could expect, after a while, that the password is again asked, just in the case an unauthorized person get access to the computer.
2. Split your app
My use case is an installer that ease the life of users by downloading, compiling and installing a software.
The previous solution would work, but I do prefer let the user run the installer, and get asked for the password when it actually hit the "Install" button.
The solution is then to split the app in 2 parts:
- The GUI
- The "engine" or command executor process
The GUI will run the "engine" using pkexec
when it needs to start it (in your case, it would need to run with pkexec
only the first time the user clicks the "Start/Stop" button), so that the GUI runs as the user, and the "engine" as root.
Now you need an inter-process communication so that the "engine" can report to the GUI, and the GUI can send commands to the "engine". To do so, the best option would be to use pipes like it is shown in this answer: https://stackoverflow.com/a/3806342/1996540.
Update
I have finally implemented my suggested answer in this project so you can have a look on a working example.