Barak
Barak

Reputation: 61

in python multiple commands using su - user

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?

Upvotes: 0

Views: 490

Answers (1)

Barak
Barak

Reputation: 61

managed to do this with creating a var

demo = "su -l username -c 'command as string'"
os.system(str(demo))

Upvotes: 2

Related Questions