#!/bin/bash
PWD=$(cd `dirname $0`; pwd)
PFDIR=/usr/local/php/php-7.4.33/sbin/php-fpm
CMD="$PFDIR -y $PWD/conf/php-fpm.conf -c $PWD/conf/php.ini"

check_status() {
  PID=`ps ux | grep "$PWD" | awk '/php-fpm.conf/ {print $2}'`
  if [ -n "$PID" ];then
    echo "php-fpm (pid $PID) is runing... " 
  else
    echo "php-fpm is stopped"
  fi
}

check_start() {
  PID=`ps ux | grep "$PWD" | awk '/php-fpm.conf/ {print $2}'`
  if [ -n "$PID" ];then
    echo "php-fpm (pid $PID) is runing... " 
    exit 1
  else
    $CMD
    check_status
  fi
}

check_stop() {
  PID=`ps ux | grep "$PWD" | awk '/php-fpm.conf/ {print $2}'`
  if [ ! -n "$PID" ];then
    echo "php-fpm is stopped"
    exit 1
  else
    kill $PID
    check_status
  fi
}

check_restart() {
  PID=`ps ux | grep "$PWD" | awk '/php-fpm.conf/ {print $2}'`
  if [ -n "$PID" ];then
    kill $PID
    check_status
  fi
  $CMD
  check_status
}

case "$1" in 
  start)
    check_start
    ;;
  stop)
    check_stop
    ;;
  restart)
    check_restart
    ;;
  status)
    check_status
    ;;
  *)
    echo "Usage:$0 start|stop|restart|status"
    ;;
esac
