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()
:
command = ['sudo', 'su', 'user', '-c', 'player --standard=1 -o 2']
If you really want to start from a string, the shlex
module can be helpful:
import shlex
command = shlex.split("sudo su user -c 'player --standard=1 -o 2'")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.