Linux限制Shell执行时间


在Linux定时任务里面,有些任务执行很频繁,必须限制在多长时间内执行完毕,否则统计结果也没有意义,可以直接将其杀死。


timeout(){
waitfor=$1
shift
command=$*
$command &
commandpid=$!

(sleep $waitfor;kill -9 $commandpid >/dev/null 2>&1) &

watchdogpid=$!
wait $commandpid >/dev/null 2>&1
kill $watchdogpid >/dev/null 2>&1
}

———这里限制myshell.sh 执行10秒  通过函数 timeout执行myshell.sh即可
timeout 10 /home/hooh/test/myshell.sh  >> /home/hooh/test/log.log

相关内容