我正在尝试创建一个将在参数上运行程序的脚本。 当程序执行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
有什么想法吗?程序午餐但不退出。
答案 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