在 运行 之后以编程方式停止 nodemon
Stop nodemon programmatically after run
我正在处理 API 在后台运行的 运行,我们有一些辅助方法需要启动 nodemon --exec babel-node commands/router.js
以显示所有路线,例如.
请注意,node commands/router.js
无法工作,因为我们需要 babel
目前,方法 运行s 没有问题,但我需要在执行后从 运行ning 停止 nodemon。我知道 nodemon 应该在执行后保持 运行ning 但我们的项目是这样设计的,我需要使用 nodemon 执行然后杀死它。
如何在 运行 之后 kill/stop nodemon?
代码
package.json
{
...
scripts: {
"start": "nodemon --exec babel-node index.js",
"router": "nodemon --exec babel-node commands/router.js"
},
...
}
router.js
const script = () => {
// Fetch and display routes
}
script()
编辑:
根据 Nodemon 文档,处理此用例的正确方法是使用 gracefulShutdown
方法手动终止进程,如下所示:
process.once('SIGUSR2', function () {
gracefulShutdown(function () {
process.kill(process.pid, 'SIGUSR2');
});
});
您可以阅读更多内容here。
我们通过删除 nodemon 并使用 process.exit()
手动结束脚本找到了解决方案
最终代码
package.json
{
...
scripts: {
"start": "nodemon --exec babel-node index.js", // <= still called with nodemon
"router": "babel-node commands/router.js"
},
...
}
router.js
const script = () => {
// Fetch and display routes
process.exit()
}
script()
我正在处理 API 在后台运行的 运行,我们有一些辅助方法需要启动 nodemon --exec babel-node commands/router.js
以显示所有路线,例如.
请注意,node commands/router.js
无法工作,因为我们需要 babel
目前,方法 运行s 没有问题,但我需要在执行后从 运行ning 停止 nodemon。我知道 nodemon 应该在执行后保持 运行ning 但我们的项目是这样设计的,我需要使用 nodemon 执行然后杀死它。
如何在 运行 之后 kill/stop nodemon?
代码
package.json
{
...
scripts: {
"start": "nodemon --exec babel-node index.js",
"router": "nodemon --exec babel-node commands/router.js"
},
...
}
router.js
const script = () => {
// Fetch and display routes
}
script()
编辑:
根据 Nodemon 文档,处理此用例的正确方法是使用 gracefulShutdown
方法手动终止进程,如下所示:
process.once('SIGUSR2', function () {
gracefulShutdown(function () {
process.kill(process.pid, 'SIGUSR2');
});
});
您可以阅读更多内容here。
我们通过删除 nodemon 并使用 process.exit()
最终代码
package.json
{
...
scripts: {
"start": "nodemon --exec babel-node index.js", // <= still called with nodemon
"router": "babel-node commands/router.js"
},
...
}
router.js
const script = () => {
// Fetch and display routes
process.exit()
}
script()