1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
kiraitachi
  • 73
  • 1
  • 4

1 Answers1

3

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • You should protect your variables with double quotes when you use `[` or `test` instead of `[[` : http://stackoverflow.com/questions/19597962/bash-illegal-number/19598570#19598570 – Idriss Neumann Jan 19 '14 at 18:51