python - 在写入其标准输入后后台进程

标签 python linux bash init zombie-process

我正在使用 linux/cpython 3.3/bash。这是我的问题:

#!/usr/bin/env python3
from subprocess import Popen, PIPE, DEVNULL
import time

s = Popen('cat', stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL)
s.stdin.write(b'helloworld')
s.stdin.close()
time.sleep(1000)     #doing stuff

这使得 cat 成为僵尸(我正忙于“做事”,不能在子进程上等待)。在 bash 中有没有一种方法可以包装 cat(例如,通过创建一个孙子),这将允许我写入 cat 的标准输入,但让 init 接受作为 parent ? python 解决方案也可以,我也可以使用 nohup、disown 等。

最佳答案

从另一个唯一任务是等待它的进程运行子进程。

pid = os.fork()
if pid == 0:
     s = Popen('cat', stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL)
     s.stdin.write(b'helloworld')
     s.stdin.close()
     s.wait()
     sys.exit()
time.sleep(1000)

关于python - 在写入其标准输入后后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650055/

相关文章:

python - CPython 中的字符串乘法是如何实现的?

python - 神经网络 - 检查节点激活

regex - 在没有 Bashisms 的情况下检查 Shell 中的子字符串

linux - 使用 find 和 sed 时出错

bash - 使用 Bash 进行函数式编程

python - 通过 SOCKs 代理请求

python - 如何在特定字符之前从字符串中删除特殊字符?

linux - 如何将一个命令的输出作为下一个命令的参数值传递

c - 如何通过pid检查子进程是否在c中完成

linux - 怎样才能找到什么^?和 ^/对应于 linux 中的键盘?