我正在尝试为我的nodeJS应用程序编写一个停止命令,该命令会greps Node 进程并杀死它,到目前为止我已经:
{
...
"scripts": {
"start": "node src/main/webapp/index.js",
"stop": "kill \"$(ps ux | grep node | grep -Eo '^\\s+[0-9]+' | tr -d '[[:space:]]')\""
}
...
}
如果我运行 ps ux
我会得到
PID PPID PGID WINPID TTY UID STIME COMMAND
12228 8428 12228 12960 cons0 197608 22:02:12 /usr/bin/ps
4840 1 4840 4840 cons0 197608 22:01:46 /usr/bin/bash
13484 12292 12292 8024 cons0 197608 22:02:07 /c/Program Files/nodejs/node
如果我运行 echo "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')"
然后我得到输出 13484
所以我知道这个位有效。但是,当我运行 npm stop
时,出现错误:
npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "stop"
npm ERR! node v0.12.4
npm ERR! npm v2.10.1
npm ERR! code ELIFECYCLE
npm ERR! app@1.0.0 stop: `kill "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app@1.0.0 stop script 'kill "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')"'.
npm ERR! This is most likely a problem with the app package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! kill "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')"
npm ERR! You can get their info via:
npm ERR! npm owner ls app
npm ERR! There is likely additional logging output above.
为什么会出现这种情况?有没有更简单的方法来做到这一点?
最佳答案
这似乎有效:
"scripts": {
"start": "node src/main/webapp/index.js & echo $! > .pid",
"stop": "kill $(cat .pid)"
}
也许这对于您想要实现的目标来说已经足够好了,但我想还有更好的方法。
关于node.js - 使用 package.json 停止脚本终止 Node 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542747/