sudo su user -c with arguments not working

2019-09-18 10:05发布

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.

标签: python linux
1条回答
男人必须洒脱
2楼-- · 2019-09-18 10:37

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'")
查看更多
登录 后发表回答