Python子进程未能正确执行

3

我遇到了输出命令到正在运行的屏幕的问题。

使用以下代码:

import subprocess
subprocess.call(["screen", "-S jcmp", "-X stuff", "'kick Jman100'`echo -ne '\015'`"])

返回以下内容:
Use: screen [-opts] [cmd [args]]
 or: screen -r [host.tty]

Options:
-a            Force all capabilities into each window's termcap.
-A -[r|R]     Adapt all windows to the new display width & height.
-c file       Read configuration file instead of '.screenrc'.
-d (-r)       Detach the elsewhere running screen (and reattach here).
-dmS name     Start as daemon: Screen session in detached mode.
-D (-r)       Detach and logout remote (and reattach here).
-D -RR        Do whatever is needed to get a screen session.
-e xy         Change command characters.
-f            Flow control on, -fn = off, -fa = auto.
-h lines      Set the size of the scrollback history buffer.
-i            Interrupt output sooner when flow control is on.
-l            Login mode on (update /var/run/utmp), -ln = off.
-list         or -ls. Do nothing, just list our SockDir.
-L            Turn on output logging.
-m            ignore $STY variable, do create a new screen session.
-O            Choose optimal output rather than exact vt100 emulation.
-p window     Preselect the named window if it exists.
-q            Quiet startup. Exits with non-zero return code if unsuccessful.
-r            Reattach to a detached screen process.
-R            Reattach if possible, otherwise start a new session.
-s shell      Shell to execute rather than $SHELL.
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title      Set title. (window's name).
-T term       Use term as $TERM for windows, rather than "screen".
-U            Tell screen to use UTF-8 encoding.
-v            Print "Screen version 4.00.03jw4 (FAU) 2-May-06".
-wipe         Do nothing, just clean up SockDir.
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute <cmd> as a screen command in the specified session.

Error: Unknown option S jcmp
1
>>> subprocess.call(["screen", "-s jcmp", "-X stuff", "'command here'`echo -ne '\015'`"])
Use: screen [-opts] [cmd [args]]
 or: screen -r [host.tty]

Options:
-a            Force all capabilities into each window's termcap.
-A -[r|R]     Adapt all windows to the new display width & height.
-c file       Read configuration file instead of '.screenrc'.
-d (-r)       Detach the elsewhere running screen (and reattach here).
-dmS name     Start as daemon: Screen session in detached mode.
-D (-r)       Detach and logout remote (and reattach here).
-D -RR        Do whatever is needed to get a screen session.
-e xy         Change command characters.
-f            Flow control on, -fn = off, -fa = auto.
-h lines      Set the size of the scrollback history buffer.
-i            Interrupt output sooner when flow control is on.
-l            Login mode on (update /var/run/utmp), -ln = off.
-list         or -ls. Do nothing, just list our SockDir.
-L            Turn on output logging.
-m            ignore $STY variable, do create a new screen session.
-O            Choose optimal output rather than exact vt100 emulation.
-p window     Preselect the named window if it exists.
-q            Quiet startup. Exits with non-zero return code if unsuccessful.
-r            Reattach to a detached screen process.
-R            Reattach if possible, otherwise start a new session.
-s shell      Shell to execute rather than $SHELL.
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title      Set title. (window's name).
-T term       Use term as $TERM for windows, rather than "screen".
-U            Tell screen to use UTF-8 encoding.
-v            Print "Screen version 4.00.03jw4 (FAU) 2-May-06".
-wipe         Do nothing, just clean up SockDir.
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute <cmd> as a screen command in the specified session.

Error: Unknown option S jcmp

"

"jcmp"是之前使用os.system()调用启动的屏幕会话的名称

"

3
kindall的回答是正确的。但如果您信任该命令,那么(只有在这种情况下),您可以使用Shell=True选项,此时您可以将命令作为单个字符串"..."提供,而不是列表[..., ...]。同样,您也可以将列表作为["bash", "-c", "您的完整命令及参数"] - anishsane
如果您不想使用shell,仍然可以使用单个字符串,并使用shlex.split将其拆分为shell所做的方式,然后将其传递给subprocess.call - kindall
1个回答

14

你收到这条消息是因为你没有正确传递参数。每个参数都应该是列表中的一个单独的字符串。也就是说,-S jcmp不应该是一个单独的参数,而应该是两个;同样适用于-X stuff

subprocess.call(["screen", "-S",  "jcmp", "-X", "stuff", "'kick Jman100'`echo -ne '\015'`"])

我有一个类似的问题,尽管我完全按照你所说的做: aws_command = ["aws","sqs","receive-message","--queue-url",queue_url,"--message_attribute_names", "data"] 但它输出错误,因为"-message_attribte_names"和"data"是未知的参数。但当直接在终端中输入时,它们确实可以正确地输出结果。 - devwanderer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接