工作中需要写个脚本,在一条命令执行后,过指定时间比如10秒后检查命令是否执行成功。
通过搜索发现两个思路
第一,
参考https://www.cnblogs.com/JeffChen/archive/2011/12/26/2600241.html
#!/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
第二,通过touch命令生成一个文件,然后检查文件是否存在
func(){
ls>>abc.txt
touch finished
}
func &
sleep 10
if [ -f finished ]
then
echo "finished"
else
echo "not finished"
fi
rm -f finished
网友评论