#!/bin/sh # set -x ROOT=$(cd `dirname $0`/../../; pwd) CURRENT_DIR=$(cd `dirname $0`; pwd) APP_NAME="Vue-dataAc-server" RUN_CMD="npm run start" STOP_CMD="npm run stop" EXE_NAME="egg-server-Vue-dataAc-server" #PID 代表是PID文件 PID=$ROOT/pid/$APP_NAME\.pid #log LOG=$ROOT/logs/$APP_NAME\.out ERROR_LOG=$ROOT/logs/$APP_NAME_error\.out usage() { echo "Usage: sh 执行脚本.sh [start|stop|restart|status]" exit 1 } #检查程序是否在运行 is_exist(){ pid=`ps -ef|grep $EXE_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } start(){ is_exist if [ $? -eq "0" ]; then echo ">>> ${EXE_NAME} is already running PID=${pid} <<<" else #nohup $EXE_NAME >> $LOG 2> $ERROR_LOG & $RUN_CMD >> $LOG 2> $ERROR_LOG echo $! > $PID echo ">>> start $EXE_NAME successed PID=$! <<<" fi } #停止方法 stop(){ $STOP_CMD >> $LOG 2> $ERROR_LOG is_exist if [ $? -eq "0" ]; then echo ">>> ${EXE_NAME} is still running PID=${pid} <<<" pidf=$(cat $PID) echo ">>> APP PID = $pidf begin kill $pidf <<<" kill $pidf rm -rf $PID sleep 2 is_exist if [ $? -eq "0" ]; then echo ">>> api 2 PID = $pid begin kill -9 $pid <<<" kill -9 $pid sleep 2 echo ">>> $EXE_NAME process stopped <<<" else echo ">>> ${EXE_NAME} is not running <<<" fi else echo ">>> ${EXE_NAME} is not running <<<" fi } #输出运行状态 status(){ is_exist if [ $? -eq "0" ]; then echo ">>> ${EXE_NAME} is running PID is ${pid} <<<" else echo ">>> ${EXE_NAME} is not running <<<" fi } #重启 restart(){ stop start } #根据输入参数,选择执行对应方法,不输入则执行使用说明 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac exit 0