Page 1 of 1

run multiple non interacticve root commands

Posted: Tue May 27, 2025 3:53 am
by BruceSteers
A class to let your program start an internal process as root and use multiple commands.

password is held in memory and used with sudo and Process.Expect() when run

Re: run multiple non interacticve root commands

Posted: Tue May 27, 2025 9:32 am
by thatbruce
Interesting! Will try it as soon as we have a need, which may be sooner than I'd like.

(Linear Programming job is progressing leaps and bounds. Pity I own none of it :( I'll just have to remember how we did it for personal use one day.)
It looks like what you did there might solve installs for the thousands of students that are going to use it. Would you believe hundreds? How about a few.

b

Re: run multiple non interacticve root commands

Posted: Tue May 27, 2025 10:50 am
by BruceSteers
if it helps 1 person i'll be happy :)

It's helped me.
I use the same class in my VBox-image mounter program. https://forum.gambas.one/viewtopic.php?t=1849

It's a Static class,
I just use RootCommand(sCommand) where it needs it (to mount/unmount the vbox drives) and it only asks for password the first time.

Re: run multiple non interacticve root commands

Posted: Tue May 27, 2025 11:33 am
by BruceSteers
I have found other ways to do this also using bash internally.

I have used either a hidden TerminalView or just Exec/Shell process to run bash as su.
hBashProcess = Shell "pkexec env XAUTHORITY=" & Env["XAUTHORITY"] & " DISPLAY=" & Env["DISPLAY"] & " bash" For Input Output As "Proc"

Then use Write/Print to send the commands through the bash process.
Print #hBashProcess, sCommand

It works but has limitations with not knowing if the commands have finished running as Process.State relates to bash not the command it's running.
Also you have to exit bash before program closes.

The above RootCommander.class is better as it uses Process.Expect() on commands run directly (using sudo) So you can Check RootCommand.Process.State to see if the individual commands have finished running or not before allowing more.

Re: run multiple non interacticve root commands

Posted: Tue May 27, 2025 3:08 pm
by gbWilly
BruceSteers wrote: Tue May 27, 2025 3:53 am A class to let your program start an internal process as root and use multiple commands.

password is held in memory and used with sudo and Process.Expect() when run
Now we're talking :lol:
I might wanna have a look at this ;)