是否可以使用Node.JS获取父进程ID?我想检测父母是否被杀死或失败,以致无法通知孩子.如果发生这种情况,子进程的父进程id应为1.
这将优于要求父母定期发送保持活动信号,并且还优于运行ps命令.
你可以使用pid文件.这样的事情
var util = require('util'),
fs = require('fs'),
pidfile = '/var/run/nodemaster.pid';
try {
var pid = fs.readFileSync(pidfile);
//REPLACE with your signal or use another method to check process existence :)
process.kill(pid, 'SIGUSR2');
util.puts('Master already running');
process.exit(1);
} catch (e) {
fs.writeFileSync(pidfile, process.pid.toString(), 'ascii');
}
//run your childs here
Run Code Online (Sandbox Code Playgroud)
你也可以在spawn()调用中发送pid作为参数
| 归档时间: |
|
| 查看次数: |
4317 次 |
| 最近记录: |