#!/bin/sh
#
# $Id: monitor_procs,v 1.5 2008/02/20 02:31:25 urabe Exp $
#
# monitor_procs list : list ps output in _win
# monitor_procs kill : kill all procs in _win
# monitor_procs test : list not-running procs in _win
# monitor_procs      : run not-running procs in _win
#
. win.conf
if [ -z "$TD" ]
then
  TD=/tmp
fi
if [ -z "$DD" ]
then
  DD=/dat
fi
tmp=$TD/monitor_procs.$$
ps agxww|sed 's/ (.*)$//'> $tmp
cat _win|grep -v '^#'|grep -v '^nice'|sed -n 's/ *&.*$//p'|expand|tr -s " "|
while read procs
do
  if [ "$1" = list ]
  then
    grep "$procs\$" $tmp
  else
    pid=`grep "$procs\$" $tmp|awk '{print $1}'`
    if [ -n "$pid" ]
    then
      if [ "$1" = kill ]
      then
        kill $pid
        echo $procs : killed \(pid=$pid\)
      fi
    else
      if [ "$1" = kill ]
      then
        :
      elif [ -z "$1" ]
      then
        $procs &
        time=`date +%y%m%d.%H%M%S`
        echo -n $time >> $DD/log/monitor_procs.log
        echo " "$procs >> $DD/log/monitor_procs.log
      else
        echo Not found : \'$procs\'
      fi
    fi
  fi
done
rm $tmp
