node.js가 'npm start'를 표현하는 앱을 중지하는 방법
Express v4.x로 node.js 앱을 빌드 한 다음 npm start로 앱을 시작 합니다. 내 질문은 앱을 중지하는 방법입니다. 거기에 NPM 정지 ?
npm 중지를 구현할 때 오류를 포함하도록 편집
/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
예, npm은 중지 스크립트도 제공합니다.
npm help npm-scripts
prestop, stop, poststop : npm stop 명령으로 실행합니다.
package.json에서 위 중 하나를 설정 한 다음 npm stop
npm help npm-stop
에서 설정하면 정말 간단하게 만들 수 있습니다 app.js
.
process.title = myApp;
그런 다음 scripts.json에서
"scripts": {
"start": "app.js"
, "stop": "pkill --signal SIGINT myApp"
}
즉, 이것이 저라면 pm2
git push를 기반으로 자동으로 처리되는 것을 사용하고 있습니다 .
여기에있는 다른 모든 솔루션은 OS에 따라 다릅니다. 모든 OS에 대한 독립 솔루션은 다음과 같이 socket.io를 사용합니다.
package.json
두 개의 스크립트가 있습니다.
"scripts": {
"start": "node server.js",
"stop": "node server.stop.js"
}
server.js-당신의 일반적인 표현이 여기에 있습니다.
const express = require('express');
const app = express();
const server = http.createServer(app);
server.listen(80, () => {
console.log('HTTP server listening on port 80');
});
// Now for the socket.io stuff - NOTE THIS IS A RESTFUL HTTP SERVER
// We are only using socket.io here to respond to the npmStop signal
// To support IPC (Inter Process Communication) AKA RPC (Remote P.C.)
const io = require('socket.io')(server);
io.on('connection', (socketServer) => {
socketServer.on('npmStop', () => {
process.exit(0);
});
});
server.stop.js
const io = require('socket.io-client');
const socketClient = io.connect('http://localhost'); // Specify port if your express server is not using default port 80
socketClient.on('connect', () => {
socketClient.emit('npmStop');
setTimeout(() => {
process.exit(0);
}, 1000);
});
그것을 테스트
npm start
(평소처럼 서버를 시작하려면)
npm stop
(이제 실행중인 서버가 중지됩니다)
위의 코드는 테스트되지 않았지만 (내 코드의 축소 된 버전이며, 내 코드는 작동합니다) 그대로 작동합니다. 어느 쪽이든 socket.io를 사용하여 서버를 중지하려는 경우 취할 일반적인 방향을 제공합니다.
제안 된 솔루션을 시도했을 때 앱 이름이 잘 렸음을 깨달았습니다. 나는 process.title
nodejs 문서 ( https://nodejs.org/docs/latest/api/process.html#process_process_title ) 에서 읽었습니다.
Linux 및 OS X에서는 argv 메모리를 덮어 쓰기 때문에 이진 이름의 크기와 명령 줄 인수의 길이로 제한됩니다.
내 앱은 인수를 사용하지 않으므로이 코드 줄을 내 app.js
process.title = process.argv[2];
and then add these few lines to my package.json
file
"scripts": {
"start": "node app.js this-name-can-be-as-long-as-it-needs-to-be",
"stop": "killall -SIGINT this-name-can-be-as-long-as-it-needs-to-be"
},
to use really long process names. npm start
and npm stop
work, of course npm stop
will always terminate all running processes, but that is ok for me.
This is a mintty version problem alternatively use cmd. To kill server process just run this command:
taskkill -F -IM node.exe
Check with netstat -nptl all processes
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1736/mongod
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1594/sshd
tcp6 0 0 :::3977 :::* LISTEN 6231/nodejs
tcp6 0 0 :::22 :::* LISTEN 1594/sshd
tcp6 0 0 :::3200 :::* LISTEN 5535/nodejs
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:
kill -9 6231
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-in process.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
import { spawn } from 'child_process'
import fs from 'fs'
const child = spawn('node', [
'dist/server.js'
], {
detached: true,
stdio: 'ignore'
})
child.unref()
if (typeof child.pid !== 'undefined') {
fs.writeFileSync('.server.pid', child.pid, {
encoding: 'utf8'
})
}
build/stop-server.js
import fs from 'fs'
import kill from 'tree-kill'
const serverPid = fs.readFileSync('.server.pid', {
encoding: 'utf8'
})
fs.unlinkSync('.server.pid')
kill(serverPid)
package.json
"scripts": {
"start": "babel-node build/start-server.js",
"stop": "babel-node build/stop-server.js"
}
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 the options.detached
argument to spawn()
and the call to child.unref()
.
On MAC OS X(/BSD): you can try to use the lsof (list open files) command
$ sudo lsof -nPi -sTCP:LISTEN
and so
$ kill -9 3320
If is very simple, just kill the process..
localmacpro$ ps
PID TTY TIME CMD
5014 ttys000 0:00.05 -bash
6906 ttys000 0:00.29 npm
6907 ttys000 0:06.39 node /Users/roger_macpro/my-project/node_modules/.bin/webpack-dev-server --inline --progress --config build/webpack.dev.conf.js
6706 ttys001 0:00.05 -bash
7157 ttys002 0:00.29 -bash
localmacpro$ kill -9 6907 6906
If you've already tried ctrl + c
and it still doesn't work, you might want to try this. This has worked for me.
Run command-line as an Administrator. Then run the below mention command. type your port number in yourPortNumber
netstat -ano | findstr :<yourPortNumber>
Then you execute this command after identify the PID.
taskkill /PID <typeyourPIDhere> /F
Kudos to @mit $ingh from http://www.callstack.in/tech/blog/windows-kill-process-by-port-number-157
You have to press combination ctrl + z My os is Ubuntu
All (3) solotion is :
1- ctlr + C
2- in json file wreite a script that stop
"scripts": { "stop": "killall -SIGINT this-name-can-be-as-long-as-it-needs-to-be" },
*than in command write // npm stop //
3- Restart the pc
참고URL : https://stackoverflow.com/questions/23258421/how-to-stop-app-that-node-js-express-npm-start
'IT story' 카테고리의 다른 글
SSL 인증서에 어떤 RSA 키 길이를 사용해야합니까? (0) | 2020.09.05 |
---|---|
ProcessBuilder와 Runtime.exec ()의 차이점 (0) | 2020.09.05 |
파이썬 문자열에서 3 개의 백 슬래시가 4와 같은 이유는 무엇입니까? (0) | 2020.09.05 |
EclipseIDE에서 클래스의 모든 메서드에 메서드 중단 점 추가 (0) | 2020.09.05 |
getAttribute () 대 Element 객체 속성? (0) | 2020.09.05 |