#!/bin/bash

DIR="/opt/pos/server/http_api"

CUR_DATE=`date +%Y%m%d_%H%M%S`
echo "$CUR_DATE >> $0" >> /opt/pos/run.log

while [ 1 ]; do
  echo "Loop..."

  RUNNING=`ps xa | grep http_api | grep -v grep | grep app.pl`

  if [ "$RUNNING" = "" ]; then
    echo "Will start API..."
    CUR_DATE=`date +%Y%m%d_%H%M%S`
    echo "$CUR_DATE >> $0 >> api is stopped, will start it..." >> /opt/pos/run.log
    ps xa | grep http_api | grep -v grep | grep -v http_api_checker | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    $DIR/run.sh &
  fi

  sleep 3
done
