一个shell启动脚本实例启动前先杀死进程

#!/bin/bash
# 启动地址路径根据swoole 启动脚本路径修改即可


basedir=$(dirname $(dirname $0))
echo $0
echo $basedir
port=9501;#端口号


check_port() {
        echo "正在检测端口check_port..."
        netstat -tlpn | grep "\b$1\b"
}
echo $1 $2

#杀死进程
stop(){
if check_port $port
then

    pid=$(lsof -F p  -i:$port | cut -b 2-);#获取进程id
    kill -9 $pid;#杀死进程
        echo "端口存在is kill-$port OK"
    exit 1
else
        echo "端口死亡 no start "
        DATE_N=`date "+%Y-%m%d %H:%M:%S"`
        echo "时间:${DATE_N}" >check_port.log #记录死亡日志
fi
}
#开启服务
start(){
    php $basedir/public/WebSocket.php
}
case "$1" in
        "start")

if check_port $port
then
    echo "is have start"
else
    echo "start"
    start 
fi
                ;;
        "stop")
                echo "stop"
                stop 
                ;;
        "reset")
                echo "reset"
                stop 
                start 
                ;;
        *)
                #其它输入
                echo "output error,please input 1/2/2"
                ;;
esac

你可能感兴趣的:(一个shell启动脚本实例启动前先杀死进程)