Killing a long running process

I'm using mplayer to transcode the mpeg2 satellite streams from my Dreambox for the kitchen radio. Since the wav stream cannot be longer than 3:06:18, and Mediatomb fails to stop the mplayer process when the radio does not use the stream anymore, I decided to write a script that kills the mplayer process automatically when it has been run for a given time.

Here is the script in case someone else has similar needs:

#!/bin/bash
#this will not work for processes running longer than 24 hours!
# time is "$days-$hours:$min:$secs" then
# the 10# is necessary because an hour or minute of "08" is
# treated as octal, which fails badly
ps -C mplayer -o "%t %c %p"| grep -v PID |while read a b p
do
 OLDIFS=$IFS
 IFS=":"
 set -- $a
 hr=10\#$1
 min=10\#$2
 sec=$3
 if [ "x$sec" = "x" ]; then
  sec=$min
  min=$hr
  hr=0
 fi
 total_min=$(( hr*60 + min))
 #echo $hr $min $sec - $total_min
 if [ "$total_min" -gt 210 ];then
  echo killing $p with $total_min minutes \($hr:$min:$sec\)
  kill -9 $p
 fi
 IFS=$OLDIFS
done

Written by Christian Weiske.

Comments? Please send an e-mail.