I am trying to execute command from python as another "user" with:
command = "sudo su user -c player --standard=1 -o 2"
subprocess.Popen(command.split(), shell=False, stdin=None, stdout=None, stderr=None, close_fds=True)
but everything after -c causes troubles. Any idea how to execute this command with arguments for my program player?
When I create separate 2.py script with "player --standard=1 -o 2" (in subprocess of course) and call this script from my first script via "sudo su user -c /home/user/2.py", it works ok.
command.split()
produces the list['sudo', 'su', 'user', '-c', 'player', '--standard=1', '-o', '2']
, which is not what you need.su
expects the command to run as a single argument after-c
.The simplest thing to do is probably to just construct the list by hand, without using
split()
:If you really want to start from a string, the
shlex
module can be helpful: