进程管理脚本包括start/stop/restart/status

#!/bin/bash
#
source /etc/profile

# 根据参数,执行进程的启动 停止 重启等

# 非apache用户运行脚本,则退出
if [ `whoami` != "apache" ];then
echo " only apache can run me"
exit 1
fi

export NODE_ENV=production


node_process='chinasoft_mindmaps'

##############node_mindmaps###############
#1.启动 node_mindmaps
start_node_mindmaps() {
    #pid=`ps -ef |grep $node_process |grep -v grep |awk '{print $2}'`
    #if [ ! "$pid" ];then
    pid=`pm2 status|grep ${node_process}|grep enabled|wc -l`
    if [[ ${pid} -lt 1 ]];then
        echo "starting node_mindmaps process is $node_process;pid is $pid "
        if [ $? -ne 0 ]; then
            echo
            exit 1
        fi
    cd /data/www/vhosts/www.chinasoft.com/httpdocs/mindmaps && npm install request && npm install && npm install --dependencies
    cd /data/www/vhosts/www.chinasoft.com/httpdocs/mindmaps && pm2 start npm --watch --name ${node_process} -- run start
        if [ $? == '0' ];then
            echo "start node_mindmaps $node_process ok"
        else
            echo "start node_mindmaps $node_process failed"
        fi
    else
        echo "node_mindmaps $node_process is still running!"
        exit
    fi
}

#2.停止 node_mindmaps
stop_node_mindmaps() {
    echo -n $"Stopping node_mindmaps $node_process: "
    #pid=`ps -ef |grep $node_process |grep -v grep |awk '{print $2}'`
    #if [ ! "$pid" ];then
    pid=`pm2 status|grep ${node_process}|grep enabled|wc -l`
    if [[ ${pid} -lt 1 ]];then
    echo "node_mindmaps $node_process is not running"
    else
    cd /data/www/vhosts/www.chinasoft.com/httpdocs/mindmaps && pm2 stop ${node_process}
    echo "stop node_mindmaps $node_process ok killed $pid"
    fi
}

#3.重启 restart_node_mindmaps
restart_node_mindmaps() {
    stop_node_mindmaps
    start_node_mindmaps
}

#4.查看 node_mindmaps 状态
status_node_mindmaps(){
    #pid=`ps -ef |grep $node_process |grep -v grep |awk '{print $2}'`
    #if [ ! "$pid" ];then
    pid=`pm2 status|grep ${node_process}|grep enabled|wc -l`
    if [[ ${pid} -lt 1 ]];then
        echo "node_mindmaps $node_process is not running"
    else
        echo "node_mindmaps $node_process is running"
    fi
}

#####################  MAIN  ###############################
usage () {
        echo ""
        echo "  Please Input server infomation!"
        echo ""
        echo "  USAGE: `basename $0` [start|stop|restart|status]" 
        echo ""
}
    

if [ $# != 1 ]
then
        usage >&2
        exit 1
fi
OPT=$1
case $OPT in
 
start)
        echo "start `basename $0`"
        start_node_mindmaps
    ;;
stop)
        stop_node_mindmaps
    ;;
restart)
        restart_node_mindmaps
    ;;
status)
        status_node_mindmaps
    ;;
*)
    echo "Usage:`basename $0`  [start|stop|restart|status]"
    exit 1
esac
原文地址:https://www.cnblogs.com/reblue520/p/13652396.html