#!/bin/ksh

#/* Part of the Maestro sequencer software package.
# * Copyright (C) 2011-2015  Operations division of the Canadian Meteorological Centre
# *                          Environment Canada
# *
# * Maestro is free software; you can redistribute it and/or
# * modify it under the terms of the GNU Lesser General Public
# * License as published by the Free Software Foundation,
# * version 2.1 of the License.
# *
# * Maestro is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# * Lesser General Public License for more details.
# *
# * You should have received a copy of the GNU Lesser General Public
# * License along with this library; if not, write to the
# * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# * Boston, MA 02111-1307, USA.
# */

#
# name: 
#
# description: simple script to verify that a program 
#              is running on the current host and starts the program if
#              it is not running. program must be in the path.
#              This script is meant to be run with hcron.
#
# set -x
this=program_monitor
arguments=$*

eval `cclargs $0 \
  -prog     ""                    ""                           "[program to monitor]" \
  -h        ""                    ""                           "[Display help       ]" \
  -logfile  ""                    ""                           "[file for logging]" \
  ++ $arguments`

if [[ "${prog}" = "" ]] ; then
   print "\nERROR: prog argument is mandatory!"
   exit 1
fi

set -x

if [[ "${logfile}" = "" ]] ; then
   logfile=/dev/null
fi

if test -z "`ps -fu ${USER}| grep -w "${prog}" | grep -v grep | grep -v ${this}`"
then
   echo `date` starting process ${prog} >> ${logfile} 2>&1
   echo "`date` which ${prog}? $(which ${prog})" >> ${logfile} 2>&1
   ${prog} > /dev/null 2>&1 &
fi
