-
def TIMEOUT_COMMAND( command, timeout) :
-
"" "call shell-command and either return its output or kill it
-
if it doesn't normally exit within timeout seconds and return None" ""
-
import subprocess , datetime , os , time , signal
-
cmd = command.split ( " " )
-
start = datetime .datetime .now ( )
-
process = subprocess .Popen ( cmd , stdout=subprocess .PIPE , stderr=subprocess .PIPE )
-
while process.poll ( ) is None :
-
time .sleep ( 0 .2 )
-
now = datetime .datetime .now ( )
-
if ( now - start) .seconds > timeout:
-
os .kill ( process.pid , signal .SIGKILL )
-
os .waitpid ( -1 , os .WNOHANG )
-
return None
-
return process.stdout .readlines ( )
- print
TIMEOUT_COMMAND(['ping','192.168.0.254'], 3)
set timeout for a shell command in python
以下就是python調用系統命令設置超時:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.