[英]How to kill a npm child process
我有一个package.json
,上面定义了debug
脚本。 该脚本启动node
应用程序。
整个npm
脚本正在由测试启动,而最后一个必须在测试结束后终止debug
脚本。
因此,当我spawn
npm run debug
并杀死它时, node
进程不会被杀死。
我试图用child_process.kill
杀死整个进程,并生成运气不好的kill
bash命令,因为pid
不属于使用npm run debug
启动的node
。
如何杀死我不拥有其pid
node
进程?
您不必一定拥有PID就能杀死它(只要运行脚本的用户有权这样做即可)。
您可以生成命令并像在命令行中那样执行命令(有多种方法)。 还有诸如find-process
类的软件包,您也可以使用它们以这种方式找到该过程。
甚至更简单的方法是在debug
启动时写一些包含pid的文件(如果可以的话)。 然后,您只需读回该文件即可获取PID。
// in debug
import { writeFile } from 'fs';
writeFile('debug.pid', process.pid, 'utf8', err => err && console.log('Error writing pid file'));
// in app where it'll kill
import { readFile } from 'fs';
let debugPid;
readFile('debug.pid', 'utf8', (err, data) => err ? console.log('Error reading pid file') : debugPid = data);
无论采用哪种方法,一旦有了PID,就可以使用process.kill()
将其杀死:
process.kill(pid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.