gpt4 book ai didi

python - 从 Python 启动一个可以解释函数和别名的 shell

转载 作者:行者123 更新时间:2023-11-28 21:19:13 25 4
gpt4 key购买 nike

我编写了一个小的 *nix 实用程序,每次检测到文件系统更改时都会“重新运行”给定的命令。所以我将命令作为引用参数传递给它,例如

rerun "my command"

Rerun 是用 Python 写的,最后调用:

subprocess.call("my command", shell=True, executable=USERS_DEFAULT_SHELL)

在我的例子中,我的默认 shell 是“/bin/bash”。但是,subprocess.call 调用的 shell 不是“交互式”shell,因此无法识别我的 .bashrc 中定义的 shell 函数和别名。

Man bash 告诉我要启动交互式 shell,我将“-i”传递给/bin/bash。但是,可以预见的是,

subprocess.call(..., executable='/bin/bash -i')

不起作用 - 它无法找到该名称的可执行文件。 (即使它确实有效,我也在尝试为用户默认的任何 shell 创建此功能,而不仅仅是 Bash。可能 '-i' 不会对所有其他 shell 执行相同的操作。)

我怎样才能从 Python 执行“我的命令”,就像用户将它输入终端时所解释的那样?

最佳答案

Posix 需要 -i shell 的选项以“交互模式”启动 shell。交互模式的精确定义因 shell 而异——显然 zshcsh不会尝试解释 .<b>bash</b>rc 中的命令-- 但使用 -i flag 应该用所有合理的 shell 做正确的事。

通常,您会通过调用 subprocess.call 来传递参数(或一些 Popen 变体)带有列表:

subprocess.call(['bash', '-i'])

当然,这不会尊重用户的 shell 偏好。你应该能够从 SHELL 得到它环境变量:

subprocess.call([os.getenv('SHELL'), '-i'])

为了让shell执行特定的命令行,你需要使用-c命令行选项,这也是 Posix 标准,因此它应该适用于所有 shell:

subprocess.call([os.getenv('SHELL'), '-i', '-c', command_to_run])

这在很多情况下都可以正常工作,但如果 shell 决定 exec,它可能会失败。 command_to_run 中的最后(或唯一)命令(有关详细信息,请参阅 this answer 上的 http://unix.stackexchange.com。)您随后尝试调用另一个 shell 来执行另一个命令。

例如,考虑简单的 python 程序:

import subprocess
subprocess.call(['/bin/bash', '-i', '-c', 'ls'])
subprocess.call(['/bin/bash', '-i', '-c', 'echo second']);

第一个bash过程开始。由于它是一个交互式 shell,它会创建一个新的进程组并将终端附加到该进程组。然后它检查要运行的命令,确定它是一个运行外部实用程序的简单命令,因此它可以 exec命令。所以它这样做了,用 ls 代替自己实用程序,现在是终端进程组的领导者。当 ls终止,终端进程组变为空,但终端仍附加到它。因此,当第二个 bash 进程启动时,它会尝试创建一个新的进程组并将终端附加到该进程组,但这是不可能的,因为终端处于一种不确定状态。根据 Posix 标准(基本定义,§11.1.2):

When there is no longer any process whose process ID or process group ID matches the foreground process group ID, the terminal shall have no foreground process group. It is unspecified whether the terminal has a foreground process group when there is a process whose process ID matches the foreground process group ID, but whose process group ID does not. No actions defined in POSIX.1-2008, other than allocation of a controlling terminal or a successful call to tcsetpgrp(), shall cause a process group to become the foreground process group of the terminal.

对于 bash,只有当字符串作为 -c 的值传递时才会发生这种情况。 argument 是一个简单的命令,因此有一个简单的解决方法:确保该字符串不是一个简单的命令。例如,

subprocess.call([os.getenv('SHELL'), '-i', '-c', ':;' + command_to_run])

在命令前添加一个空操作,使其成为一个复合命令。但是,这不适用于其他在尾调用优化方面更具侵略性的 shell。所以一般的解决方案需要遵循 Posix 建议的路径,同时注意 tcsetpgrp 的描述。系统调用:

Attempts to use tcsetpgrp() from a process which is a member of a background process group on a fildes associated with its controlling terminal shall cause the process group to be sent a SIGTTOU signal. If the calling thread is blocking SIGTTOU signals or the process is ignoring SIGTTOU signals, the process shall be allowed to perform the operation, and no signal is sent.

由于对 SIGTTOU 的默认操作signal是停止进程,我们需要忽略或者阻塞这个signal。所以我们最终得到以下结果:

!/usr/bin/python
import signal
import subprocess
import os

# Ignore SIGTTOU
signal.signal(signal.SIGTTOU, signal.SIG_IGN)

def run_command_in_shell(cmd):
# Run the command
subprocess.call([os.getenv('SHELL'), '-i', '-c', cmd])
# Retrieve the terminal
os.tcsetpgrp(0,os.getpgrp())

run_command_in_shell('ls')
run_command_in_shell('ls')

关于python - 从 Python 启动一个可以解释函数和别名的 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099895/

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