[英]Hung shell script when called from python
python pythontop.py # Hangs in test.sh. python pythontop.py#挂在test.sh中。 cat of urandom is not truncated at all
乌兰多姆猫根本没有被截断
pythontop.py pythontop.py
import subprocess
subprocess.call(['test.sh'])
test.sh test.sh
RAND=`cat /dev/urandom | tr -dc 'a-zA-Z0-9'| fold -w 10 | head -n 1`
EDIT:Folks apologies for any confusion. 编辑:对任何混乱表示歉意。 It turns out simply calling the shell script from python itself causes the script to hang.
事实证明,仅从python本身调用shell脚本会导致脚本挂起。
UPDATE: More update. 更新:更多更新。 If I insert a tee before head.
如果在头前插入一个T恤。 I get a broken pipe message.
我收到一条坏消息。 Which means head did its job.
这意味着负责人做好了工作。 Got a line and closed the file.
一行并关闭文件。
cat /dev/urandom | tr -dc 'a-zA-Z0-9'| fold -w 10 | tee | head -n 1
I see the script still hung even after the broken pipe message! 即使管道消息断开,我仍然看到脚本仍然挂起!
UPDATE: Folks, I find this easily recreatable in linuxcontainers.org machine. 更新:伙计们,我发现这很容易在linuxcontainers.org机器上重新创建。 I did this so that those reading this question can confirm this observation with their own experiments.
我这样做是为了让阅读此问题的人可以通过自己的实验来确认这一观察结果。 Since this seems a little too hard to be true.
由于这似乎有点难以置信。 https://linuxcontainers.org/lxd/try-it/
https://linuxcontainers.org/lxd/try-it/
Although your code works just fine for me on my laptop, it seems to hang in a containerized environment, such as linux containers . 尽管您的代码对我的笔记本电脑来说工作正常,但它似乎挂在了容器化的环境中,例如linux container 。
Changing 改变中
subprocess.call(['test.sh'])
to 至
subprocess.call(['/bin/bash', 'test.sh'], shell=True)
stops the script from hanging. 停止挂起脚本。
The subprocess documentation advises against using shell=True
on arbitrary unsanitized input from untrusted sources, so you have been warned. 子流程文档建议不要对来自不受信任来源的任意未经消毒的输入使用
shell=True
,因此已警告您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.