Bash脚本启动程序并在给定时间退出

时间:2014-01-19 17:35:46

标签: linux bash shell time kill

我正在尝试创建一个将在参数上运行程序的脚本。 当程序执行1分钟后,程序将退出。

我的代码如下所示:

pid=$(pidof $1)
true=1
$1
while [ $true = 1 ]
 do
time=$(ps -p $pid -o etime=)
if  $time > 01:00 
 then
  true=0
  kill -9 $pid
  echo "The process $pid has finish since the execution time has ended"
fi
done

有什么想法吗?程序午餐但不退出。

1 个答案:

答案 0 :(得分:3)

实际上你的问题是这一行:

if  $time > 01:00 

由于$time无法与01:00

进行比较

您需要先将时间转换为秒,如下所示:

time=$(ps -p $pid -o etime= | awk -F ':' '{print $1*60 + $2}')
if [[ $time -gt 60 ]]; then
    # your if block code here
fi
相关问题