shell timeout
寫(xiě)腳本的時(shí)候,經(jīng)常需要用到超時(shí)控制。看《shell專家編程》時(shí)看到一個(gè)好例:修改了一下,
1.超過(guò)timeout時(shí)間還沒(méi)執(zhí)行完,則kill進(jìn)程,發(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
}
#測(cè)試的函數(shù)
test123()
{
??????? sleep 20
}
timeout test123
2.超過(guò)timeout時(shí)間還沒(méi)執(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的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: reinterpret_cast和sta
- 下一篇: 滤波电容的选择(调试中)