Andre M
Andre M

Reputation: 7534

Stopping nodejs process start by npm?

I am in the process of creating some scripts to deploy my node.js based application, via continuous integration and I am having trouble seeing the right way to stop the node process.

I start the application via a start-dev.sh script:

#!/bin/sh

scripts_dir=`dirname $0`
cd "${scripts_dir}/"..

npm start &
echo $! > app.pid

And then I was hoping to stop it via:

#!/bin/sh

scripts_dir=`dirname $0`
cd "${scripts_dir}/"..

echo killing pid `cat app.pid`
kill -9 `cat app.pid`

The issue I am finding is that npm is no longer running at this point, so the pid isn't useful to stop the process tree. The only workaround I can think of at this point is to skip npm completely for launch and simply call node directly?

Can anyone suggest an appropriate way to deal with this? Is foregoing npm for launching a good approach, in this context?

Upvotes: 0

Views: 228

Answers (2)

Matt
Matt

Reputation: 74620

Forever can do the process management stuff for you.

forever start app.js
forever stop app.js

Try to avoid relying on npm start outside of development, it just adds an additional layer between you and node.

Upvotes: 1

Rahul Kamboj
Rahul Kamboj

Reputation: 469

just use supervisor example conf is like

[program:long_script]
command=/usr/bin/node SOURCE_FOLDER/EXECUTABLE_JAVASCRIPT_FILE.js 
autostart=true
autorestart=true
stderr_logfile=/var/log/long.err.log
stdout_logfile=/var/log/long.out.log

where

SOURCE_FOLDER is the folder for your project

EXECUTABLE_JAVASCRIPT_FILE the file to be run

you can check the post here

Upvotes: 0

Related Questions