shell timeout
寫腳本的時候,經(jīng)常需要用到超時控制??础秙hell專家編程》時看到一個好例:修改了一下,
1.超過timeout時間還沒執(zhí)行完,則kill進程,發(fā)郵件告警:
?
set-x
mailSend()
{
??????? mailContent="xxxx Web response time over 5 seconds"
??????? echo $mailContent | mail -s "xxxxxx Web TimeOut"xxxxx@xxx.cion
}
timeout()
{
??????? waitfor=3
??????? command=$*
??????? $command &
??????? commandpid=$!
??????? ( sleep $waitfor ; kill -9 $commandpid? >/dev/null2>&1&& mailSend )&
??????? watchdog=$!
??????? sleeppid=$PPID
??????? wait $commandpid >/dev/null2>&1
??????? kill $sleeppid >/dev/null2>&1
}
#測試的函數(shù)
test123()
{
??????? sleep 20
}
timeout test123
2.超過timeout時間還沒執(zhí)行完,只發(fā)郵件告警,程序正常執(zhí)行:
mailSend()
{
??????? mailContent="xxxxe Web response time over 5 seconds,Please have a check !"
??????? echo $mailContent | mail -s "xxxxx WEB response time over 5 senconds" $mailTo
}
timeout()
{
??????? waitfor=6
??????? command=$*
??????? $command &
??????? commandpid=$!
??????? ( sleep $waitfor ;? mailSend )&
??????? watchdog=$!
??????? sleeppid=$PPID
??????? wait $commandpid >/dev/null2>&1
??????? kill? -9 $watchdog >/dev/null2>&1
??????? kill $sleeppid >/dev/null2>&1
}
總結(jié)
以上是生活随笔為你收集整理的shell timeout的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: reinterpret_cast和sta
- 下一篇: 滤波电容的选择(调试中)