|
藍森林 http://www.lslnet.com 2006年6月26日 11:18
請教:如何讓shell超時退出?
如何能讓一個shell腳本運行5秒,5秒後無論是否執行完畢都中止呢?
我的腳本是這樣:
#!/bin/sh
while [ 1 ] ;do
lynx -dump -connect_timeout=5 http://wap.mon.com |grep serverd
if [ $? -eq 1 ]
then
echo " Alert!: Unable to connect to remote host."
fi
sleep 10;
done
當和對方服務器無法建立連接時,connect_timeout參數起作用,會結束lynx。但有時對方服務器能建立連接但不返回頁面,這時lynx就會一直等待下去。
我想問:怎樣能讓這個lynx5秒後,無論什麼情況都退出??? |
請教:如何讓shell超時退出?
再寫一個腳本做監控,如果該程序運行5秒就kill掉它! |
請教:如何讓shell超時退出?
有沒有自己退出的? |
請教:如何讓shell超時退出?
2樓的辦法不太理想,繼續等 |
請教:如何讓shell超時退出?
如何寫腳本監控?
還請不吝賜教 :) |
請教:如何讓shell超時退出?
#!/bin/sh
while [ 1 ];do
TIME=`ps -e -o pid -o etime -o args |grep "wap" |grep lynx |awk '{print $2}' |awk -F: '{print $2}'`
PID=`ps -e -o pid -o etime -o args |grep "grep" |grep lynx |awk '{print $1}'`
if [ $TIME -gt 5 ]
then
kill -9 $PID
fi
sleep 5
done
給樓上的,大家還有什麼好辦法?總覺的這樣有點彆扭 :( |
請教:如何讓shell超時退出?
是有點彆扭 |
請教:如何讓shell超時退出?
not tested ...
#!/bin/sh
echo $$ > /tmp/pid.log
sleep 5 && kill `cat /tmp/pid.log &
#--start ur code---#
#
#
#--end ur code----# |
請教:如何讓shell超時退出?
typo , missing close " `" ,
corrected one :
sleep 5 && kill `cat /tmp/pid.log` & |
請教:如何讓shell超時退出?
也可以考慮用stty來實現限時
[code]stty -icanon min 0 time 50[/code] |
請教:如何讓shell超時退出?
反正根據我的以前的實驗結果,那個sleep 5很多時候都不準時。有的時候遠遠超過5秒了,都一點反應沒有。 |
請教:如何讓shell超時退出?
我正在寫一個script
利用 fd , pipe 機制
類似於如此
[code]
#!/bin/ksh
function myproc {
#do something here
}
#set timeout
TIMEOUT=5
#make pipe & fd
infile=infile.tmp
rm $infile 2>/dev/null
mknod $infile p
exec 8<>$infile
#run script in backgound
myproc <&8 &
#kill when timeout,^C stand for ctrl + V ,then ctrl + C
sleep $TIMEOUT && echo ^C > $infile
...
[/code] |
| |