Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python bash脚本中的coreutils超时对应用程序不透明_Python_Linux_Bash_Terminal_Timeout - Fatal编程技术网

Python bash脚本中的coreutils超时对应用程序不透明

Python bash脚本中的coreutils超时对应用程序不透明,python,linux,bash,terminal,timeout,Python,Linux,Bash,Terminal,Timeout,我在bash脚本中通过/usr/bin/timeout执行应用程序时遇到问题。 在本例中,这是一个简单的python结构脚本(结构版本1.14) 为了安装这个版本的结构库,可以运行:pip install“fabric,只需使用python中的时间模型,就可以在不使用bash的情况下使用timeout import time time.sleep(5) #change the 5 by the seconds that you need to set a timeout 您需要使用--前台选

我在bash脚本中通过/usr/bin/timeout执行应用程序时遇到问题。 在本例中,这是一个简单的python结构脚本(结构版本1.14)
为了安装这个版本的结构库,可以运行:pip install“fabric,只需使用python中的时间模型,就可以在不使用bash的情况下使用timeout

import time

time.sleep(5)
#change the 5 by the seconds that you need to set a timeout

您需要使用
--前台
选项调用
超时

timeout --foreground ./test.py
只有当
timeout
命令不是从交互式shell执行时(即,如果它是从脚本文件执行的),才需要这样做

超时
信息页面中引用:

‘--foreground’
     Don’t create a separate background program group, so that the
     managed COMMAND can use the foreground TTY normally.  This is
     needed to support timing out commands not started directly from an
     interactive shell, in two situations.
       1. COMMAND is interactive and needs to read from the terminal for
          example
       2. the user wants to support sending signals directly to COMMAND
          from the terminal (like Ctrl-C for example)
本例中实际发生的情况是,fabric(或某个调用的东西)正在调用
tcsetattr
来关闭终端echo。我不知道为什么,但我想这与用于(而不是)收集用户密码的过程有关。(我只是在一个屏幕上看到了它;我没有尝试找到该调用。)尝试从后台进程更改tty配置将导致进程阻塞,直到它重新控制tty,这就是发生的情况

如果不使用
timeout
,则不会发生这种情况,因为
bash
不会创建后台程序组。我认为结构2可以避免调用
tcsetattr

您也可以通过避免基于密码的SSH身份验证来避免这个问题,但我没有尝试


您还可以通过将
stdin
重定向到
/dev/null
(在
timeout
命令或shell脚本调用中)来避免问题。如果您不需要将stdin转发到远程命令(您可能不需要),这可能也很有用。

我猜您的意思是signal.alarm().time.sleep()不会终止应用程序使用更好的sys.exit()coreutils timeout和signal.alarm()的目的是在特定时间后向应用程序发送信号,除非应用程序自行终止。time.sleep()和sys.exit()不能替代!sys.exit()将立即终止python脚本,而不让它运行特定的时间。您能否运行
env-i bash--norc
以获得一个干净的shell,并验证您的命令在手动执行时仍然可以正常工作?是的,仍然可以正常工作:35; env-i bash--norc bash-4.4#timeout 10./test.py&&echo“RETCODE=$?”[localhost]运行:echo Hello![localhost]out:Hello![localhost]out:RETCODE=0 bash-4.4#太好了。现在可以运行
echo'timeout 10./test.py&&echo“RETCODE=$?“'>myfile&&env-i bash--norc myfile
以验证同一类型shell中的脚本中该确切命令是否失败?是否有一个脚本失败以及
env-i bash--norc-c”超时10./test.py&&echo“RETCODE=$?”
失败,请您进行深入分析。添加--前台选项确实有帮助(应用程序正常运行)。将stdin重定向到/dev/null
超时10 python./test.py 0>/dev/null
会导致应用程序崩溃,出现python IOError异常。用密钥身份验证替换密码身份验证没有帮助。根据您的分析,我仍然不太清楚的是,只有在使用bash脚本中的超时工具调用应用程序时,才能观察到问题,并且从命令行调用应用程序时,即使使用超时工具,仍然可以正常工作。哦,当从命令行调用时,前台可能只是默认的,在bash脚本中有不同的终端设置。前景与背景和终端设置不完全是我的世界。@fakej:重定向方向相反:
timeout 10 python./test.py噢,我的错,谢谢你指出这一点。我承认它也很管用!
[root@testhost:~ ] $ cat test.py
#!/usr/bin/python
from fabric.api import run, settings

with settings(host_string='localhost', user='root', password='XXXXX'):
    run('echo Hello!')
[root@testhost:~ ] $
import time

time.sleep(5)
#change the 5 by the seconds that you need to set a timeout
timeout --foreground ./test.py
‘--foreground’
     Don’t create a separate background program group, so that the
     managed COMMAND can use the foreground TTY normally.  This is
     needed to support timing out commands not started directly from an
     interactive shell, in two situations.
       1. COMMAND is interactive and needs to read from the terminal for
          example
       2. the user wants to support sending signals directly to COMMAND
          from the terminal (like Ctrl-C for example)