How Kill node process on killing forever process
13,454
Yes there is, use a signal handler in your script to catch the sigterm and kill the node process.
www.gnu.org/software/bash/manual/html_node/Signals.html
Author by
Aayush
Updated on June 15, 2022Comments
-
Aayush almost 2 years
I have a script to start and stop my node.js server. When I stop the script, the forever process is killed however the node process is not terminated.
Is there any way to stop both forver and node process when I issue
Kill $FOREVER_PID
Here is the script -
#!/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
The final script working for me -
#!/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
-
Aayush over 10 yearsFound a way. Not the optimal solution, I guess but it works for the meanwhile. I am fetching the node PID using
ps aux | grep node | grep -v grep | awk '{print $2}'
and then kill the node process. -
robertklep over 10 yearsWhy not use forever's built-in functionality to manage scripts?
-
Aayush over 10 yearskillall node works but has the same problem as
ps aux | grep node | grep -v grep | awk '{print $2}'
. What if there are more than one instances of node. Suppose one for logging to mongo and another one a rest-api, for example -
krouis over 10 yearsinstead of
ps aux | grep node | grep -v grep | awk '{print $2}'
you may usepidof node
orpgrep node
to get the list of pids. You may also consider usingpkill node
instead ofkill $(pidof node)
. -
rafa.ferreira over 8 years
-