hyperf 服務管理指令碼 start、restart、stop、status

h1712715552發表於2020-01-06

新建檔案 bin/server.sh

#!/bin/bash
basepath=$(cd `dirname $0`; cd ..; pwd)
serverfile="${basepath}/bin/hyperf.php"

cd $basepath

# 重置檔案快取
initproxy(){
  rm -rf runtime/container
  echo "Runtime cleared"
  php "${serverfile}" di:init-proxy
  echo "Init proxy runtime created"
  return $!
}

# 停止服務
stop(){

  # 判斷主程式如果存在
  if [ -f "runtime/hyperf.pid" ];then
    cat runtime/hyperf.pid | awk '{print $1}' | xargs kill && rm -rf runtime/hyperf.pid
  fi

  # 判斷是否有殘留程式,通知退出
  local num=`count`
  while [ $num -gt 0 ]; do
    echo "The worker num:${num}"
    ps -ef | grep "${serverfile}" | grep -v "grep"| awk '{print $2}'| xargs kill
    num=`count`
    sleep 1
  done

  echo "Stop!"
  return $!
}

# 程式數
count()
{
  echo `ps -fe |grep "${serverfile}" | grep -v "grep"| wc -l`
}

#檢視狀態
status(){
  local num=`count`
  if [ $num -gt 0 ];then
    if [ -f "runtime/hyperf.pid" ];then
      local pid=" pid:`cat runtime/hyperf.pid | awk '{print $1}'`"
    fi
    echo "Running!${pid} worker num:${num}"
  else
    echo "Close!"
  fi
  return $!
}

# 啟動服務
start()
{
  local num=`count`
  if [ $num -gt 0 ];then
    status
    return $!
  fi
  initproxy
  exec php "${serverfile}" start
  echo "Start!"
  return $!
}

# 幫助文件
help()
{
    cat <<- EOF
    Usage:
        help [options] [<command_name>]Options:
    Options:
        stop      Stop hyper server
        start     Start hyper server
        restart   Restart hyper server
        status    Status hyper server check
        initproxy Init proxy runtime created
        help      Help document
EOF
    return $!
}

case $1 in
  'stop')
    stop
  ;;
  'start')
    start
  ;;
  'restart')
    stop
    start
  ;;
 'status')
    status
  ;;
 'initproxy')
    initproxy
  ;;
  *)
    help
  ;;
esac

exit 0
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章