简体   繁体   English

从python调用时挂起shell脚本

[英]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.

相关问题 从python脚本调用时,从shell脚本返回值 - Return a value from shell script when it is called from a python script 当 python 脚本在 Z2591C98B70119FE62489Z 脚本 BEE4 - Pass the variables from python script to shell script when python script is being called inside shell script 当在python脚本内部调用shell脚本时,如何将变量从python传递给shell脚本 - how to pass variable from python to shell script when shell script is getting called inside python script 从shell脚本调用时访问python脚本中的环境变量不同于直接在shell中运行它 - accessing environment variable in python script when called from a shell script differs from running it in the shell directly 在python脚本结束后,从python调用的shell脚本是否仍然存在? - Will shell scripts called from python persist after the python script ends? 通过PHP调用Python脚本时将挂起shell_exec() - Python Script Hangs When Called via PHP shell_exec() 管道到头导致从 python 调用的 shell 脚本中的管道损坏 - Piping to head results in broken pipe in shell script called from python 在从Python子进程调用的脚本中模拟shell命令.Popen() - Mock a shell command in a script called from Python subprocess.Popen() python脚本会引发错误,具体取决于如何从shell调用它 - python script raises an error depending on how it was called from the shell 从cmuri shell中由mercurial调用的python脚本运行.exe - Run .exe from a python script called by mercurial in cmd shell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM