从python调用时挂起的shell脚本

时间:2017-11-21 08:58:03

标签: python linux shell

python pythontop.py#在test.sh中挂起urandom的猫根本没有被截断

pythontop.py

import subprocess
subprocess.call(['test.sh'])

test.sh

RAND=`cat /dev/urandom | tr -dc 'a-zA-Z0-9'| fold -w 10 |  head -n 1`
编辑:人们对任何混乱道歉。事实证明,简单地从python本身调用shell脚本会导致脚本挂起 更新: 更多更新。如果我在头前插入一个发球台。我得到了一个破碎的管道消息。这意味着head完成了它的工作。得到一行并关闭文件。
cat /dev/urandom | tr -dc 'a-zA-Z0-9'| fold -w 10 | tee | head -n 1我看到即使在管道信息破碎后脚本仍然挂起!
更新: 伙计们,我发现这很容易在linuxcontainers.org机器上进行修复。我这样做是为了让那些阅读这个问题的人能够通过自己的实验证实这一观察结果。因为这似乎有点难以成真。 https://linuxcontainers.org/lxd/try-it/

1 个答案:

答案 0 :(得分:0)

虽然您的笔记本电脑上的代码对我来说效果很好,但它似乎仍然存在于容器化的环境中,例如linux containers

更改

subprocess.call(['test.sh'])

subprocess.call(['/bin/bash', 'test.sh'], shell=True)

停止脚本挂起。

subprocess documentation建议不要对来自不受信任来源的任意未经过抽取的输入使用shell=True,因此您已收到警告。

相关问题