I am trying to execute a command in python 3.6
as a different user with popen
from subprocess
but it will still execute as the user who called the script (i plan to call it as root). I am using threads and therefore it is important that i don't violate the user rights when 2 threads execute in parallel.
proc = subprocess.Popen(['echo $USER; touch myFile.txt'],
shell=True,
env={'FOO':'bar', 'USER':'www-data'},
stdout=subprocess.PIPE)
The example above will still create the myFile.txt
with my user_id 1000
I tried different approaches :
tried with as described in Run child processes as different user from a long running Python process by copying the os.environment and changed the user, etc
(note this is for python 2)tried with as described in https://docs.python.org/3.6/library/subprocess.html#popen-constructor by using
start_new_session=True
My Last option is to prefix the command with sudo -u username command
but i don't think this is the elegant way.
`demote(33,33)`
`