我正在尝试杀死我在服务器中运行的一个子进程。基本上,子进程运行johnny-five代码,这是我在React到我的服务器的在线终端中编写的代码。当我运行子进程时,代码运行得很好,但是如果我想要杀死子进程,我必须停止服务器。我尝试过用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')
})
})
`
发布于 2019-03-21 03:07:45
您可以使用linux命令行。
要使用该命令查看正在运行的进程,请使用:
pgrep node要终止进程,可以使用以下命令:
kill <pid>或者强制关机
kill -9 <pid>或者,如果您想要杀死所有节点进程
kill $(pgrep node) https://stackoverflow.com/questions/55267989
复制相似问题