gpt4 book ai didi

python - 无法使用 subprocess.call 执行 "sudo su - postgres"

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:55 24 4
gpt4 key购买 nike

我尝试以下命令:

subprocess.call(['sudo', 'su - postgres'], shell=True)

subprocess.call(['sudo', 'su', '-', 'postgres'], shell=True)

在 Python2.7 中(通过 ipython 手动编写行,或者 python myfile.py 作为代码的一部分作为行),并获取 sudo 用法信息:

usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user
name|#uid]
usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U user
name] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u
user name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u
user name|#uid] file ...

我可以毫无问题地在 shell 中运行命令。两次都是同一个 shell。

:我做错了什么?

最佳答案

来自subprocess docs :

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

因此,您的列表实际上是一个序列。对于单个命令,只需将字符串放在一起,因为不需要将其拆分:

subprocess.call(['sudo su - postgres'], shell=True)

关于python - 无法使用 subprocess.call 执行 "sudo su - postgres",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com