#!/bin/sh
timeout()
{
waitfor=3
command=$*
$command &
commandpid=$!
( sleep $waitfor ; kill -9 $commandpid > /dev/null 2>&1 ) &
watchdog=$!
sleeppid=$PPID
#原文中这句应该是有误,为什么要这么获取父进程ID?
#sleeppid=`ps $ppid $watchdog | awk '{print $1}'`
#下面一行加上重定向是避免在被KILL的时候,报出被kill的提示
#当然带来的副作用是重定向不一定符合预期
wait $commandpid > /dev/null 2>&1
kill $sleeppid > /dev/null 2>&1
}
test123()
{
sleep 10
}
timeout test123
转:http://www.boluor.com/shell%E8%84%9A%E6%9C%AC%E8%B6%85%E6%97%B6%E6%8E%A7%E5%88%B6.html