2024-03-05
PHP
hyperf3.1
797

一、启动脚本

1.在项目根目录创建文件(.env 同级目录)
vim hyperf.sh
2.脚本内容
#!/bin/bash

# 进程名称
projectName=$(grep "^APP_NAME" .env | cut -d '=' -f2)

# 停止服务
stop() {
  # 判断主进程如果存在
  if [ -f "runtime/hyperf.pid" ]; then
    cat runtime/hyperf.pid | awk '{print $1}' | xargs kill -9 && rm -rf runtime/hyperf.pid && rm -rf runtime/container
  fi

  # 判断是否有残留进程,通知退出
  local num=$(count)
  echo "Stopping ${projectName}"

  while [ $num -gt 0 ]; do
    echo "The child worker num:${num}"
    ps -ef | grep "${projectName}" | grep -v "grep" | awk '{print $2}' | xargs kill -9
    num=$(count)
    sleep 1
  done

  echo "Stopped!"
  return $!
}

# 进程数
count() {
  echo $(ps -fe | grep "${projectName}" | 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}"
    ps -ef | grep "${projectName}" | grep -v "grep"
  else
    echo "Closed!"
  fi

  return $!
}

# 启动服务
start() {
  echo "Starting ${projectName}"

  local num=$(count)
  if [ $num -gt 0 ]; then
    status
    return $!
  fi

  rm -rf runtime/container
  php82 bin/hyperf.php start

  echo "Started!"
  return $!
}

# 帮助文档
help() {
  cat <<-EOF
    Usage:
        help [options] [<command_name>]Options:
    Options:
        stop      Stop hyperf server
        start     Start hyperf server
        restart   Restart hyperf server
        status    Status hyperf server check
        help      Help document
EOF
  return $!
}

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

exit 0
3.使用方法
sh hyperf.sh
sh hyperf.sh status
sh hyperf.sh start
sh hyperf.sh restart
sh hyperf.sh stop
标签:

hyperf3.1