I am running a script as userA with root access, from this script I want to make a popen() call and run a different process as userB.
os.setuid() does not seem to work for this (unless I am doing this wrong?), and I would like to avoid a linux based solution such as su -userB -c <command> 
Is there a pythonic way of running a process as userB while the script is running as userA?
The following answer has a really nice approach for this: https://stackoverflow.com/a/6037494/505154
There is a working code example there, but the summary is to use subprocess.Popen() with a preexec_fn to set up the environment of the subprocess so that it executes as another user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With