我有一个需要收听端口的应用程序(比如8080)。有时当我运行它时,已经有一个进程列出要在该端口上请求。 因此,我创建了一段Node.js代码,用于查找给定命令(例如node或Phyton)在给定端口上运行的进程。
它找到进程的PID并使用process.kill
它尝试在启动新的“服务器”列表到端口之前终止任务。但是它失败。
var lsof = require('lsof');
var info = "%s \"%s\" (pid:%s) in state \"%s\" running on port %s Launced by %s";
/*
options:
options.ports = A comma separated list of ports you want to look format ["8001, 8080, 8000"]
options.command = A comma separated list of task names which have running tasks (on given ports) ["node,Python"]
*/
module.exports = (function(options) {
// lsof.raw(function(data) {
// console.log(data);
// });
options = options || {};
options.ports = (typeof options.ports !== "undefined") ? options.ports.toString() : "8001, 8080, 8000";
options.commands = (typeof options.commands !== "undefined") ? options.commands.toString() : "node,Python";
options.commands = options.commands.split(",");
options.ports = options.ports.split(",");
console.log("Will test port(s) %s for task(s) started by %", options.ports, options.commands);
options.ports.map(function(port) {
lsof.rawTcpPort(port, function(tasks) {
console.log("checking port %d found %d tasks started by %s", port, tasks.length, options.commands.join(" or "));
tasks.filter(function(task) {
console.log(info, "Found task ", task.name, task.pid, task.state, port, task.command);
return options.commands.indexOf(task.command) > -1;
})
.map(function(task) {
console.log(info, "Send command SIGINT to task ", task.name, task.pid, task.state, port, task.command);
process.kill(task.pid, "SIGINT");
});
});
});
})();
我做错了什么?