I'm trying to run commands after running su - username
I tried to use:
subprocess.run(['su', '-', str(username)]) #username is var
subprocess.run(['touch', 'test.txt'])
or
run_su = 'su - '+ str(username) #username is var
os.system(run_su)
or
subprocess.call(['su', '-', str(username)]) #username is var
subprocess.call(['touch', 'test.txt'])
or
p1 = subprocess.Popen(['su', '-', str(username)], shell=True, stdout=subprocess.PIPE) #username is var
p2 = subprocess.Popen(['touch', 'test.txt'], stdin=p1.stdout, stdout=subprocess.PIPE)
nothing seems to work for me.
can you suggest a way to do that?
touchcommand will not be executed until the shell you start withsuis terminated, and it will not be executed by thesuprocess. You need to pass the command to be executed tosu. Why not use a sehllscript?sucommand terminates, since it tries to open a new shell. It will either terminate by closing that shell, or by typing a wrong password.chownthe file to the desired user.