进程管理脚本包括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

 直接集群模式启动的方法

$ cat /usr/local/worksh/jenkins_rsync/node_pixso_help_manager.sh
#!/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='pixso-help'

program_dir="/data/www/vhosts/pixso.chinasoft.cn/httpdocs/help/"

#3.重启 restart_pixso-help
restart_pixso_help() {
    cd ${program_dir} && npm i
    cd ${program_dir} && pm2 start
    cd ${program_dir} && pm2 restart pixso-help
    if [[ $? == '0' ]];then
        sleep 2
        pid=`pm2 status|grep pixso-help|grep online|wc -l`
        pro_num=`sudo /usr/bin/netstat -tnlp|grep 3010|wc -l`
        if [[ $pid -ge 1 && $pro_num -gt 0 ]];then
            echo "start pixso-help $node_process ok"
        else
            echo "start pixso-help $node_process failed"
        fi
    fi
}

#4.查看 pixso-help 状态
status_pixso_help(){
    pid=`pm2 status|grep pixso-help|grep online|wc -l`
    if [[ ${pid} -lt 1 ]];then
        echo "pixso-help $node_process is not running"
    else
        echo "pixso-help $node_process is running"
    fi
}

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

if [ $# != 1 ]
then
        usage >&2
        exit 1
fi
OPT=$1
case $OPT in

restart)
        restart_pixso_help
    ;;
status)
        status_pixso_help
    ;;
*)
    echo "Usage:`basename $0`  [restart|status]"
    exit 1
esac
$ pm2 status
┌─────┬───────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name              │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ pixso-convert     │ default     │ N/A     │ fork    │ 21457    │ 0s     │ 202… │ online    │ 0%       │ 18.5mb   │ apache   │ disabled │
│ 1   │ pixso-convert     │ default     │ N/A     │ fork    │ 21451    │ 0s     │ 202… │ online    │ 0%       │ 18.9mb   │ apache   │ disabled │
│ 2   │ pixso-convert     │ default     │ N/A     │ fork    │ 21306    │ 2s     │ 202… │ online    │ 0%       │ 35.6mb   │ apache   │ disabled │
│ 4   │ pixso-convert     │ default     │ N/A     │ fork    │ 21322    │ 2s     │ 407… │ online    │ 0%       │ 36.0mb   │ apache   │ disabled │
│ 5   │ pixso-convert     │ default     │ N/A     │ fork    │ 21377    │ 1s     │ 407… │ online    │ 0%       │ 35.9mb   │ apache   │ disabled │
│ 6   │ pixso-convert     │ default     │ N/A     │ fork    │ 21388    │ 1s     │ 228… │ online    │ 0%       │ 35.8mb   │ apache   │ enabled  │
│ 3   │ pixso-feedback    │ default     │ 2.0.0   │ cluster │ 6591     │ 14D    │ 31   │ online    │ 0%       │ 164.6mb  │ apache   │ disabled │
│ 7   │ pixso-help        │ default     │ 2.0.0   │ cluster │ 12182    │ 2m     │ 1    │ online    │ 0%       │ 107.5mb  │ apache   │ enabled  │
└─────┴───────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

 调试方法,手动启动会把详细信息打出来

node /data/www/vhosts/www.chinasoft.com/httpdocs/templates/node_modules/.bin/nuxt start

 

要添加apache用户关于netstat的sudo权限

apache ALL=NOPASSWD: /usr/bin/netstat

依赖的文件

# cat /data/www/vhosts/mm.chinasoft.cn/community/ecosystem.config.js 
module.exports = {
    apps: [
        {
            name: 'community-cn-master',
            exec_mode: 'cluster',
            instances: 'max', // Or a number of instances
            script: './node_modules/nuxt/bin/nuxt.js',
            args: 'start'
        }
    ]
}

 

posted @ 2020-09-11 16:23  reblue520  阅读(329)  评论(0编辑  收藏  举报