8

我有一个脚本来启动和停止我的 node.js 服务器。当我停止脚本时,永远的进程被终止,但节点进程没有终止。

当我发出时,有什么方法可以同时停止 forver 和 node 进程

Kill $FOREVER_PID

这是脚本 -

    #!/bin/bash
path="/Users/aayush/Desktop/node/rest-api"
action="forever errorLog_express.js "

logFile="$path/system.log"
pidFile="$path/pidFile.pid"

#messages
usage="Usage : node-script.sh start|stop"
panic="Panic! nothing to do, exiting"
unknown="Unrecognized parameter"
start="[starting node-forever]"
end="[stopping node-forever]"
notRunning="Process node-forever not running"
alreadyRunning="Process node-forever already running"

if [ -z $1 ]
then
    echo $panic
    echo $usage
    exit 0;
fi

if [ $1 = "start" ]
then
    # starting process
    dummy="OK"
    if [ -f $pidFile ];
    then
        exit 0
    else
        cd $path
        echo "cd $path"
        echo $start
        echo $start >> $logFile
        $action > /dev/null 2>&1 &
        Process_Pid=$!
        echo $Process_Pid > $pidFile
        echo $dummy
        exit 0
    fi
elif [ $1 = "stop" ]
then
    # stopping process by getting pid from pid file
    dummy="OK"
    echo $end
    echo $end >> $logFile
    if [ -f $pidFile ];
    then
        while IFS=: read -r pid
        do
            # reading line in variable pid
            if [ -z $pid ]
            then
                dummy="FAILED"
                echo "Could not parse pid PANIC ! do 'ps' and check manully"
            else
                echo "Process Pid : $pid"
                kill $pid
            fi
        done <"$pidFile"
        rm $pidFile
        echo $dummy
        exit 0
    else
        echo $notRunning
        echo "FAILED"
        exit 0
    fi
else
    echo $unknown
    echo $usage
    exit 0
fi

为我工作的最终脚本 -

#!/bin/bash
#proccessname: node

USER=node
PWD=node
node=node
forever=forever
path="/Users/aayush/Desktop/node/rest-api"
action="forever start -l forever.log -a -o out.log -e err.log errorLog_express.js "

start(){
cd $path
$action
     }

stop(){
  /usr/local/bin/forever stopall
 }

restart(){
stop
start
}

status(){
/usr/local/bin/forever list
}

#Options 

case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    start
    ;;
    status)
    status
    ;;
    *)
    echo $ "usage $0 {start | stop | status | restart}"

    exit 1
esac
exit 0
4

2 回答 2

2

是的,在您的脚本中使用信号处理程序来捕获 sigterm 并终止节点进程。

www.gnu.org/software/bash/manual/html_node/Signals.html

于 2013-12-23T14:19:47.897 回答
-1
$ killall node

会杀了他们。

于 2017-01-20T11:39:03.707 回答