gpt4 book ai didi

node.js - 如何杀死 Node 进程永远杀死进程

转载 作者:搜寻专家 更新时间:2023-10-31 22:25:55 24 4
gpt4 key购买 nike

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

有没有办法在我发出时同时停止 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

最佳答案

是的,在脚本中使用信号处理程序来捕获信号项并终止 Node 进程。

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

关于node.js - 如何杀死 Node 进程永远杀死进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19136396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com