我必须在pid和port以及路径上找到机器上所有正在运行的node.js应用,然后按照路径查找所有类,然后在类中查找函数。示例假设我们有2个单独的具有不同文件夹的独立节点应用程序,并希望如上所述找到它。我努力但没有找到解决方案。我必须为客户端项目实现此功能。这可能吗?如果可能的话,该怎么做?
任何见识都会受到赞赏。
谢谢!感谢您的帮助
答案 0 :(得分:2)
是的,有两种方法可以做到这一点-
pgrep node
ps ax
将提供进程的完整列表,并添加'u'选项将提供详细信息。即ps aux
为了搜索特定的过程,使用了grep
命令,因此对于nodejs,它将是ps aux | grep node
netstat -a | more
:同时显示监听和非监听套接字
netstat -at
:列出所有tcp端口。
netstat -l
:仅列出侦听端口。
netstat -lt
:仅列出正在监听的tcp端口。
netstat -pt
:显示PID和程序名称
因此,chris-lam建议netstat -lntp | grep node
将列出所有作为节点进程运行的侦听TCP端口。
在代码后面的代码中使用它可能会有所帮助-
const {exec} = require('child_process');
exec('ps aux | grep node', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
答案 1 :(得分:1)
您可以使用netstat
命令找到端口和PID。
netstat -lntp | grep node
ps
可让您找到该过程的完整命令:
ps aux | grep node