javascript - 在 Node.js 中杀死一个子进程,同时仍在运行服务器
问题描述
我试图杀死我在服务器中运行的子进程。基本上,子进程运行我在 React 的在线终端中编写的 johnny-5 代码到我的服务器。当我运行子进程时,代码运行良好,但是如果我想杀死子进程,我不能在不停止服务器的情况下这样做。我试过这样做Control-C
,.exit()
但似乎都不管用。
codeRouter
.post('/codeAPI', (req, res) => {
console.log(req.body)
let fileName = `johnnyFiles/${req.body.currentFile}`
fs.writeFileSync(fileName, req.body.currentCode, (err) => {
if (err) throw err
})
let id = shortid.generate()
let fileObject = {
fileName: req.body.currentFile,
fileContents: req.body.currentCode,
ID: id
}
data = [fileObject, ...data]
fs.writeFileSync('data/fileData.json', JSON.stringify(data), (err) => {
if (err) throw err
})
res.json(data)
///////////////////////////////////////////
let nodeSpawn = spawn('node', [fileName], {
//detached: true,
shell: true
})
nodeSpawn.stdout.on('data', (data) => {
console.log("OUTPUT", data.toString())
})
nodeSpawn.stderr.on('data', (data) => {
console.log("ERRORS", data.toString())
})
nodeSpawn.on('exit', (code) => {
console.log(`Child exited with code ${code}`)
nodeSpawn.kill('SIGINT')
})
})
`
解决方案
您可以使用 linux 命令行。
要使用命令查看正在运行的进程,请使用:
pgrep node
要终止进程,您可以使用:
kill <pid>
或者强制关机
kill -9 <pid>
或者如果你想杀死所有节点进程
kill $(pgrep node)
推荐阅读
- javascript - 使用自定义 `tslint` 规则,无需从 typescript 编译
- audio - 从 Arduino 中的 FFT 检测特定模式
- sql - 如果类型为“10”,则不显示客户名称“a”
- javascript - 从字节缓冲区转换为字符串,然后在 NodeJS / Javascript 中转换回字节
- python - “CommentForm”对象没有属性“cleaned_data”
- database - 在线程中检查与数据库的连接
- wordpress - 哪些 OpenStreetMap 瓦片服务器用于 WordPress 插件开发?
- java - 无法从 DAP 上的 http 响应读取缓冲区中的特殊字符
- c++ - 对成员函数和变量的未定义引用
- vue.js - 如何在 Vuex 中正确填充对象数组?