写了个shell控制swoole服务器的启动、停止、重启,这样不必每次定位到swoole的目录中执行一些操作。
创建shell脚本文件:
nano swoole.sh
shell如下:
#!/bin/bash
COMMAND=$1
PORT=3848 #Swoole服务监听端口
SERVER=/system/libraries/Location/entrance #Swoole文件路径,可以不带.php(文件名后缀也需要删除)
if [ $# -lt 1 ]; then
echo "Error: at least one parameter is required"
exit 1
fi
ProcessID() {
PROCESS=`lsof -t -i:$PORT`
return 0
}
function KILLING() {
ProcessID
if [ ${#PROCESS} -eq 0 ]; then
echo "Notice: no running services"
if [ $COMMAND = "restart" ]; then
echo "starting..."
fi
return 0
fi
kill $PROCESS
}
if [ $COMMAND = "start" -o $COMMAND = "restart" ]; then
if [ $COMMAND = "start" ]; then
ProcessID
if [ ${#PROCESS} -ne 0 ]; then
echo "Notice: services already running"
exit 0
fi
fi
if [ -z $2 ];then
DOCUMENT=$PWD
else
DOCUMENT=$2
fi
if [ ! -f $DOCUMENT$SERVER ]; then
echo "Fatal error: tcp server does not exists"
exit 1
fi
if [ $COMMAND = "restart" ];
then
KILLING
sleep 1
fi
php8.0 $DOCUMENT$SERVER
exit 0
elif [ $COMMAND = "stop" ]; then
KILLING
exit 0
elif [ $COMMAND = "reload" ]; then
ProcessID
if [ ${#PROCESS} -eq 0 ]; then
echo "Notice: no running services"
exit 0
fi
MASTER=""
eval $(echo $PROCESS | awk '{ printf("MASTER=%s",$1)}')
$(kill -USR1 $MASTER)
$(kill -USR2 $MASTER)
else
echo "usage: start|restart|stop|reload"
exit 1
fi
基本使用方法:
./swoole.sh start #启动
./swoole.sh restart #重启
./swoole.sh stop #停止
./swoole.sh reload #重载配置
用法及注意事项:
1、默认将swoole.sh文件所在目录视为根目录
2、shell脚本内可以配置swoole文件路径,还可以通过命令行中的第二个参数,改变文件路径,例如:
./swoole.sh start /project/swoole.php
通过上述命令,将启动/project/swoole.php的swoole服务
进阶用法:
swoole.php文件,可将.php去掉,保留swoole即可
swoole.sh文件,可将.sh去掉,保留swoole即可
创建软连接
ln -s /swoole.sh路径 /usr/bin/swoole
在任意目录下可执行
swoole start #启动
swoole restart #重启
swoole stop #停止
swoole reload #重载配置