swoole项目服务管理脚本 (类似于nginx/php-fpm)

导语

看着nginx mysql php-fpm 都自有相关的服务管理脚本,都是服务器端服务,很多swooler都是人肉来进行操作,也有很多朋友在群里问这方面的问题,所以这里分享一下我的经验。

目标

实现 start|stop|force-quit|restart|reload|reloadtask|status 这些命令

思路

在进程名上设置服务相关的信息,然后跟据进程名来或取相关进程的id

要点

假设我现在的服务叫zmail_server (邮件发送服务:https://github.com/shenzhe/zmail)

1) 主进程:在onStart回调,通过swoole_set_process_name进行进程名设置 

关键代码:

swoole_set_process_name(' zmail_server running tcp://0.0.0.0:9501 master:" . $server->master_pid);

2) 管理进程:在onManagerStart回调,通过swoole_set_process_name进行进程名设置 

关键代码:

swoole_set_process_name(' zmail_server manager:" . $server->manager_id);

完整脚本,按需修改

#! /bin/sh

### BEGIN INIT INFO

# Provides:          mail_server

# Required-Start:    $remote_fs $network

# Required-Stop:     $remote_fs $network

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: starts mail_server

# Description:       starts the PHP FastCGI Process Manager daemon

### END INIT INFO

#php路径,如不知道在哪,可以用whereis php尝试

PHP_BIN=/usr/local/php/bin/php


#代码根目录

SERVER_PATH=/home/jing.wang/mail

getMasterPid()

{

    PID=`/bin/ps axu|grep zmail_server|grep master|awk '{print $2}'`

    echo $PID

}


getManagerPid()

{

    MID=`/bin/ps axu|grep zmail_server|grep manager|awk '{print $2}'`

    echo $MID

}

case "$1" in

        start)

                PID=`getMasterPid`

                if [ -n "$PID" ]; then

                    echo -n "mail server is running"

                    exit 1

                fi

                echo -n "Starting mail server "

                ####下面一行修改为你的php运行脚本#########

                $PHP_BIN $SERVER_PATH/webroot/main.php default  

                echo " done"

        ;;


        stop)

                PID=`getMasterPid`

                if [ -z "$PID" ]; then

                    echo -n "mail server is not running"

                    exit 1

                fi

                echo -n "Gracefully shutting down mail server "


                kill $PID

                echo " done"

        ;;

        status)

                PID=`getMasterPid`

                if [ -n "$PID" ]; then

                    echo -n "mail server is running"

                else

                    echo -n "mail server is not running"

                fi

        ;;

        force-quit)

                $0 stop

        ;;

        restart)

                $0 stop

                $0 start

        ;;

        reload)

                MID=`getManagerPid`

                if [ -z "$MID" ]; then

                    echo -n "mail server is not running"

                    exit 1

                fi

                echo -n "Reload service mail_server "

                kill -USR1 $MID

                echo " done"

        ;;

        reloadtask)

                MID=`getManagerPid`

                if [ -z "$MID" ]; then

                    echo -n "mail server is not running"

                    exit 1

                fi


                echo -n "Reload service mail_server"


                kill -USR2 $MID


                echo " done"

        ;;


        *)

                echo "Usage: $0 {start|stop|force-quit|restart|reload|reloadtask|status}"

                exit 1

        ;;


esac

开机自启

  1. 把这个脚本,放到/etc/init.d/zmail_server下

  2. chmod +x /etc/init.d/zmail_server  使其可执行

  3. chkconfig --add zmail_server  加入开机启动

运行结果

执行:/etc/init.d/mail_server start

终于可以和nginx/php-fpm 一样管理了。

你可能感兴趣的:(swoole)