Question

I'm trying to make a script that will run a program on parameter. When the program has been executed for 1 minute the program will exit.

My code looks like this:

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

Any ideas? Program lunches but does not quit.

Was it helpful?

Solution

Actually your problem is this line:

if  $time > 01:00 

As $time cannot be compared against 01:00

You need to first convert the time into seconds like this:

time=$(ps -p $pid -o etime= | awk -F ':' '{print $1*60 + $2}')
if [[ $time -gt 60 ]]; then
    # your if block code here
fi
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top