#!/bin/bash
#usage: ./watch.sh port appname "startcmd"
#param1: port, listenning by watched process
#param2: appname, the appname
#param3: startcmd, the launch command
#example: ./watch.sh 9999 gds "gds -d ./ -p 127.0.0.1:9990 -l /tmp/gds.log -D -H 0/0"
result=$(lsof -i :$1 | grep -w "$2" | grep -v vi | grep -v grep| grep -v "/bin/bash" | grep -v "/bin/sh" | wc -l)
if [ X"$result" == X"0" ]; then
  echo "$(date) $2 at port $1 is missing, try to launch again."  >> $GDS_INSTALL_DIR/logs/watch.log
  $($3)
fi
