You build node.js app with express v4.x then start your app by npm start. My question is how to stop the app? Is there npm stop?
EDIT to include the error when implement npm stop
/home/nodetest2# npm stop
> nodetest2@0.0.1 stop /home/nodetest2
> pkill -s SIGINT nodetest2
pkill: invalid argument for option 's' -- SIGINT
npm ERR! nodetest2@0.0.1 stop: `pkill -s SIGINT nodetest2`
npm ERR! Exit status 2
If is very simple, just kill the process..
Check with netstat -nptl all processes
And it simply kills the process by the PID reference.... In my case I want to stop the 6231/nodejs so I execute the following command:
Here's another solution that mixes ideas from the previous answers. It takes the "kill process" approach while addressing the concern about platform independence.
It relies on the tree-kill package to handle killing the server process tree. I found killing the entire process tree necessary in my projects because some tools (e.g.
babel-node
) spawn child processes. If you only need to kill a single process, you can replace tree-kill with the built-inprocess.kill()
method.The solution follows (the first two arguments to
spawn()
should be modified to reflect the specific recipe for running your server):build/start-server.js
build/stop-server.js
package.json
Note that this solution detaches the start script from the server (i.e.
npm start
will return immediately and not block until the server is stopped). If you prefer the traditional blocking behavior, simply remove theoptions.detached
argument tospawn()
and the call tochild.unref()
.When I tried the suggested solution I realized that my app name was truncated. I read up on
process.title
in the nodejs documentation (https://nodejs.org/docs/latest/api/process.html#process_process_title) and it saysMy app does not use any arguments, so I can add this line of code to my
app.js
and then add these few lines to my
package.json
fileto use really long process names.
npm start
andnpm stop
work, of coursenpm stop
will always terminate all running processes, but that is ok for me.On MAC OS X(/BSD): you can try to use the lsof (list open files) command
and so
This is a mintty version problem alternatively use cmd. To kill server process just run this command: