我有一个脚本来启动和停止我的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
答案 0 :(得分:1)
是的,在脚本中使用信号处理程序来捕获sigterm并终止节点进程。
www.gnu.org/software/bash/manual/html_node/Signals.html
答案 1 :(得分:0)
$ killall node
会杀了他们。