python - 与 child 的 child 互动

标签 python linux subprocess file-descriptor


我需要直接与使用 subprocess 生成的进程的 stdinstdout 进行交互。我可以这样做:

proc = subprocess.Popen("/bin/bash", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())

但是,一旦进程 fork ,我就无法再与其交互。我有这个测试程序:

// child.c
#include <unistd.h>
int main() {
    execve("/bin/bash", NULL, NULL);
}

我用它编译:

gcc -o child child.c

当我在 Popen 调用中使用 child 而不是 /bin/bash 尝试上面的 python 代码时,我得到一个 破管错误。

我尝试了什么:
由于 python 创建的文件描述符默认情况下不可继承,我尝试更改它:

inr, inw = os.pipe()
outr, outw = os.pipe()
os.set_inheritable(inr, True)
os.set_inheritable(inw, True)
os.set_inheritable(outr, True)
os.set_inheritable(outw, True)

proc = subprocess.Popen("./child", stdin=inr, stdout=outw, stderr=outw)
proc.stdin.write(b"whoami\n")
print(proc.stdout.readline())

os.close(inr)
os.close(inw)
os.close(outr)
os.close(outw)

但我仍然得到同样的错误。我认为这应该是一件容易的事,而且我可能遗漏了一些东西。任何帮助表示赞赏。

[编辑] 在我编辑我的帖子之前,我使用 test 而不是 child 作为我的测试 c 可执行文件的名称。我了解到它确实是我的 PATH 中的一个程序,位于 /usr/bin/test.
将可执行文件名称更改为 child 成功解决了我的问题。经过几个小时的反复试验,我知道答案会很简单……
非常感谢 Barmar 指出这一点!

最佳答案

当您调用 test 时,您调用的是 /bin/test,而不是您的程序。

要么重命名您的 C 程序,使其不与标准命令冲突,要么使用路径名访问它,这样它就不会在 $PATH 中搜索它。

proc = subprocess.Popen("./test", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

关于python - 与 child 的 child 互动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54917200/

相关文章:

linux - 为什么我的 init.d start-stop-daemon 脚本不能在启动时启动应用程序,但我可以手动启动该服务?

Python、solr 海量查询 : need some suggestions

python - 在 Python 数据框中获取一天的一部分(早上、下午、晚上、晚上)

python - 轮廓和轮廓f的使用

python - 从 np.datetime64 转换为浮点年份

linux - 如何启动多个 xterm 窗口并在每个窗口上运行命令,然后让每个窗口保持打开状态?

Python:如何将二进制数据写入标准输出以便 bash 脚本可以使用进程替换?

python - 使用Python的subprocess模块​​通过命令行参数运行程序

python - 从 Python 中的 imagemagick 子进程读取输出

python - 在 foreach 循环中运行多个子进程?一次一个?