1. 程式人生 > 實用技巧 >程序管理指令碼包括start/stop/restart/status

程序管理指令碼包括start/stop/restart/status

cat /usr/local/worksh/node_chinasoft_temp_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='chinasoft_templates'

##############node_chinasoft_templastes###############
#
1.啟動 node_chinasoft_templastes start_node_chinasoft_templastes() { #pid=`ps -ef |grep $node_process |grep -v grep |awk '{print $2}'` pid=`pm2 status|grep chinasoft_templates|grep enabled|wc -l` if [[ ${pid} -lt 1 ]];then echo "starting node_chinasoft_templastes process is $node_process;pid is $pid
" if [ $? -ne 0 ]; then echo exit 1 fi cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && pm2 start npm --watch --name chinasoft_templates -- run start if [ $? == '0' ];then echo "start node_chinasoft_templastes $node_process ok
" else echo "start node_chinasoft_templastes $node_process failed" fi else echo "node_chinasoft_templastes $node_process is still running!" exit fi } #2.停止 node_chinasoft_templastes stop_node_chinasoft_templastes() { echo -n $"Stopping node_chinasoft_templastes $node_process: " #pid=`ps -ef |grep $node_process |grep -v grep |awk '{print $2}'` #if [ ! "$pid" ];then pid=`pm2 status|grep chinasoft_templates|grep enabled|wc -l` if [[ ${pid} -lt 1 ]];then echo "node_chinasoft_templastes $node_process is not running" else cd /data/www/vhosts/www.chinasoft.com/httpdocs/templates && pm2 stop chinasoft_templates echo "stop node_chinasoft_templastes $node_process ok killed $pid" fi } #3.重啟 restart_node_chinasoft_templastes restart_node_chinasoft_templastes() { stop_node_chinasoft_templastes start_node_chinasoft_templastes } #4.檢視 node_chinasoft_templastes 狀態 status_node_chinasoft_templastes(){ #pid=`ps -ef |grep $node_process |grep -v grep |awk '{print $2}'` #if [ ! "$pid" ];then pid=`pm2 status|grep chinasoft_templates|grep enabled|wc -l` if [[ ${pid} -lt 1 ]];then echo "node_chinasoft_templastes $node_process is not running" else echo "node_chinasoft_templastes $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_chinasoft_templastes ;; stop) stop_node_chinasoft_templastes ;; restart) restart_node_chinasoft_templastes ;; status) status_node_chinasoft_templastes ;; *) echo "Usage:`basename $0` [start|stop|restart|status]" exit 1 esac