代码改变世界

Linux shell定时器

2011-12-26 16:02  Jeff  阅读(11706)  评论(1编辑  收藏  举报
网上找了个例子,简单实用如下. 可以有一个后台进程,在timer超时采取一些操作.
 
#!/bin/ksh
#Delaytime=0
AlarmHandler()
{
echo "Get SIGALAM"
KillSubProcs
exit 14
}
KillSubProcs()
{
#kill cbm
echo "it's time out,kill cbm here"
if [ $? -eq 0 ];then
echo "Sub-processes killed."
fi
}

SetTimer()
{
Delaytime=$1
echo $Delaytime
if [ $Delaytime  -ne 0 ];then
sleep $Delaytime  && kill -s 14 $$ &
#CHPROCIDS="$CHPROCIDS $!"
TIMERPROC=$!
fi
}
UnsetTimer()
{
echo "Start to unset timer"
kill $TIMERPROC
}

trap AlarmHandler 14
SetTimer 30
#CHPROCIDS="$CHPROCIDS $!"
#wait $!
sleep 40
UnsetTimer
echo "ALL Done."
exit 0