I am trying to run a shell (bash) command using Python’s subprocess module. However, I want to do this as another user. I am running my python script as root but my subprocess shell command needs to run as testuser. I have tried using:
-
Subprocess args since I am using Python 3.9:
subprocess.Popen(cmd, user="testuser", shell=True, stdout=subprocess.PIPE)
-
Demoting user before running the command:
subprocess.Popen(cmd, preexec_fn=demote(1000), ...)
However, none of these seem to work. I am seeing the output of my command run as if it were root and not testuser.
1