hyperf框架启动服务脚本-mac系统中调试

Hyperf 框架时,在本地调试的时候,经常要用到启动的命令,比较麻烦,根据网络的脚本优化了一下,进行记录一下以免后续找不到

使用方法

1.在项目根目录,创建新的脚本文件 server.sh
2.设置执行权限 chmod u+x server.sh
3.设置脚本内容

#!/bin/bash

sys=$(uname)
#处理mac系统和linux系统目录情况
if [[ $sys =~ "Darwin"  ]] || [[ $sys =~ "Linux"  ]];
then
	base_path=$(cd `dirname $0`; pwd)
else
  #默认win系统应为win一般没有uname命令
  base_path=$(cd $1; pwd)
fi


server_file="${base_path}/bin/hyperf.php"
pid_path="${base_path}/runtime/hyperf.pid"
php_path=$(which php)
env_path="${base_path}/.env"
runtime_container="${base_path}/runtime/container"


cd $base_path

function console_blue() {
    echo -e "\033[36m[ $1 ]\033[0m"
}

function console_green() {
    echo -e "\033[32m[ $1 ]\033[0m"
}

function console_orangered() {
    echo -e "\033[31m\033[01m[ $1 ]\033[0m"
}

function console_yellow() {
    echo -e "\033[33m\033[01m[ $1 ]\033[0m"
}

if [[ ! -f "$env_path" ]]
then
    console_orangered 'You should copy the .env.example file and name it .env or create a new file and rename .env'
    exit 1
fi

source "$env_path"

project_name=${APP_NAME}

if [[ ! -f "$php_path" ]];
then
    console_orangered 'Please check if the PHP has been installed '
    exit 1
fi

function master_process_ppid() {
    if [[ -f "${pid_path}" ]];
    then
        hyperf_pid=`cat ${pid_path}`
        echo `ps -ef | grep "${hyperf_pid}" | grep -v grep | awk '{print $2}'`
    fi
}

function master_process_count() {
    if [[ -f "${pid_path}" ]];
    then
        hyperf_pid=`cat ${pid_path}`
        echo `ps -ef | grep "${hyperf_pid}" | grep -v grep | wc -l`
    fi
}

function master_process_name_count() {
    echo `ps -ef | grep "${project_name}.Master" | grep -v grep | wc -l`
}

function fetch_server_master_pid() {
    echo `ps -ef | grep "${project_name}.Master" | grep -v grep | awk '{print $1}'`
}

function force_kill() {
 
    #多个进程取最后一个
    local process_ppid=`echo $(master_process_ppid) | awk '{print $NF}'`;
    ps -ef | grep "${process_ppid}" | grep -v grep | awk '{print $2}' | xargs kill -9 >> /tmp/hyperferror.log 2>&1
}

function status() {
    local process_count=$((`master_process_count`+`master_process_name_count`))
    if [[ $process_count -eq 0 ]]
    then
        console_yellow 'The server has been stopped !'
        exit 0
    fi
    console_green "The server is running ! Master pid is ` cat "${pid_path}"`"
    exit 0
}

function stop() {
    local process_count=$((`master_process_count`+`master_process_name_count`))
    if [[ $process_count -eq 0 ]]
    then
        console_yellow 'The server has been stopped !'
        exit 0
    fi
    if [[ -f "${pid_path}" ]]
    then
        force_kill
        cat "${pid_path}" | awk '{print $1}' | xargs kill -9 && rm -rf "${pid_path}"
    fi
    if [[ $(master_process_name_count) -gt 0 ]]
    then
        force_kill
    fi
    console_green 'The server is stopped !'
}

function start() {
    local process_count=$((`master_process_count`+`master_process_name_count`))
     echo ${project_name} >> /tmp/hyperferror.log 2>&1
    if [[ $process_count -gt 0 ]]
    then
        console_yellow "The server has been started, Don't need start again ! "
        console_blue "Master pid is : ` cat "${pid_path}"` "
        exit 0
    fi

    rm -rf "$runtime_container"

    console_blue 'Starting now, please just a moment !'
     php "${server_file}" start >> /tmp/hyperferror.log 2>&1 &
    sleep 1

    console_green "Started successful !"
    if [[ -f "${pid_path}" ]]
    then
        console_blue "Master pid is : `cat ${pid_path}` "
    fi

}

function help() {
    cat <<- EOF
    Usage:
        ./server.sh [options]
    Options:
        stop        Stop server
        start       Start server
        restart     Restart server
        status      Check server status
        help        Help document
EOF
}

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

exit 0

4.脚本命令

# 查看 hyperf 服务状态
./server.sh status

# 开启 hyperf 服务
./server.sh start

# 关闭 hyperf 服务
./server.sh stop

# 重启 hyperf 服务
./server.sh restart
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值