如何获取列表 nodejs processes/app 端口和路径?

How to get list nodejs processes/app port and path?

作为一名自由职业者 SysAdmin/Developer 我需要 get/update 现有 app/web 在客户端服务器上。有时我需要更新我不知道端口或路径的现有 NodeJS 应用程序。

如果我想知道端口,我可以通过使用 apache2ctl -S 命令找到 apache2/PHP 应用程序配置来轻松完成。但是即使我知道端口也很难找到 nodeJS 应用程序路径。

通常节点应用程序是 运行 使用 pm2。 pm list 仅显示进程列表 name/command,如下所示:

root@lamp-s-4vcpu-8gb-sgp1-01:~# pm2 list
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 11 │ .com               │ fork     │ 0    │ stopped   │ 0%       │ 0b       │
│ 6  │ DEV API            │ fork     │ 0    │ stopped   │ 0%       │ 0b       │
│ 0  │ balance Web        │ fork     │ 7    │ stopped   │ 0%       │ 0b       │
│ 4  │ balance queue      │ fork     │ 0    │ stopped   │ 0%       │ 0b       │
│ 9  │ index              │ fork     │ 133… │ online    │ 0.3%     │ 144.0mb  │
│ 10 │ index              │ fork     │ 21   │ online    │ 0.2%     │ 134.5mb  │
│ 12 │ index              │ fork     │ 13   │ online    │ 0.4%     │ 155.4mb  │
│ 7  │ longbgst           │ fork     │ 52   │ online    │ 0%       │ 40.0mb   │
│ 8  │ npm                │ fork     │ 220… │ stopped   │ 0%       │ 0b       │
│ 2  │ npm run staging    │ fork     │ 116… │ stopped   │ 0%       │ 0b       │
│ 1  │ old                │ fork     │ 0    │ stopped   │ 0%       │ 0b       │
│ 5  │ v-api dev          │ fork     │ 0    │ stopped   │ 0%       │ 0b       │
│ 3  │ develop API        │ fork     │ 358… │ stopped   │ 0%       │ 0b       │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'

pm2 show命令只显示路径而不是端口示例:

root@lamp-s-4vcpu-8gb-sgp1-01:~# pm2 show 10
 Describing process with id 10 - name index
┌───────────────────┬─────────────────────────────────────────┐
│ status            │ online                                  │
│ name              │ index                                   │
│ namespace         │ default                                 │
│ version           │ 1.0.0                                   │
│ restarts          │ 21                                      │
│ uptime            │ 2D                                      │
│ script path       │ /var/www/api-path-id/dist/index.js      │
│ script args       │ N/A                                     │
│ error log path    │ /root/.pm2/logs/index-error.log         │
│ out log path      │ /root/.pm2/logs/index-out.log           │
│ pid path          │ /root/.pm2/pids/index-10.pid            │
│ interpreter       │ node                                    │
│ interpreter args  │ N/A                                     │
│ script id         │ 10                                      │
│ exec cwd          │ /var/www/api-path-id                    │
│ exec mode         │ fork_mode                               │
│ node.js version   │ 11.15.0                                 │
│ node env          │ api_path_id                             │
│ watch & reload    │ ✘                                       │
│ unstable restarts │ 0                                       │
│ created at        │ 2020-11-16T03:29:39.945Z

通常我需要使用 pm2 show 命令为每个 pm2 进程查找应用程序路径,然后找到路径并在 .env/config 文件上找到端口。如果有这么多进程,那将花费很长时间。有没有更快的方法来查看 运行 nodejs 应用程序列表以及端口和路径?

我尝试了来自 this question answer 的命令,但没有返回路径或进程名称,如下所示:

root@lamp-s-4vcpu-8gb-sgp1-01:~# ss -tnlp | grep "node /"
LISTEN   0         128               127.0.0.1:6030             0.0.0.0:*        users:(("node /var/www/v",pid=26000,fd=25))
LISTEN   0         128                 0.0.0.0:6031             0.0.0.0:*        users:(("node /var/www/a",pid=26006,fd=25))
LISTEN   0         128               127.0.0.1:6039             0.0.0.0:*        users:(("node /var/www/a",pid=26013,fd=25))
root@lamp-s-4vcpu-8gb-sgp1-01:~#
root@lamp-s-4vcpu-8gb-sgp1-01:~# ss -ntlp | grep $(pm2 ls | grep "SITENAME" | awk '{print }') | awk '{print }'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

ss -lntp | grep node 命令显示所有端口但给出了错误的路径(可能是 cutted/substr)。

root@lamp-s-4vcpu-8gb-sgp1-01:~# ss -lntp | grep node
LISTEN   0         128               127.0.0.1:6030             0.0.0.0:*        users:(("node /var/www/v",pid=26000,fd=25))
LISTEN   0         128                 0.0.0.0:6031             0.0.0.0:*        users:(("node /var/www/a",pid=26006,fd=25))
LISTEN   0         128               127.0.0.1:6039             0.0.0.0:*        users:(("node /var/www/a",pid=26013,fd=25))
LISTEN   0         128               127.0.0.1:3001             0.0.0.0:*        users:(("node",pid=29886,fd=24))
root@lamp-s-4vcpu-8gb-sgp1-01:~# cd /var/www/a
-bash: cd: /var/www/a: No such file or directory

我需要端口和路径列表(或至少 pm2 进程名称)。

您仍然需要发出 pm2 list 命令来获取 app_name 但这可能会给您一些帮助。

通过以下步骤,您可以设置一个脚本来遍历从命令 pm2 pid 返回的每个 PID's。这可以通过将输出存储在文件 pid.text 并循环遍历来完成。

pm2 pid > pid.text

第 1 步 - 获取 PID

TEST_PID=`pm2 pid app_name`

<app_name>是pm2进程的名称。 PID 现在将在 TEST_PID

步骤 2 - 获取脚本的路径

TEST_PATH=$(ps -f -p $TEST_PID | grep -o "/.*")

这将使用在第一步中获得的PID 并在整个进程列表中搜索它。然后它将其通过管道传输到 grep 中,我们只希望使用 -o 选项进行精确的文本匹配。我们正在寻找的模式是一个 / 来指示目录的开始,后跟任何字符,我们想要它后面的所有内容。

步骤 3 - 获取端口

可以使用netstat命令获取端口信息。这是通过知道我们有 PID

这一事实来完成的
PORT_TEST=$(sudo netstat -p | grep $TEST_PID | grep ':[0-9]*\s')

第 4 步 - 打印出来

echo -e $TEST_PID'\n'$TEST_PATH'\n'$PORT_TEST
319
/home/warren/Ec2/EC2-QueryData/index.js
tcp6 0 0 XXX.XXX.XXX.XXX:33979 TUDBNXJL63:52025 ESTABLISHED 319/node

我刚学了一些 Linux 命令来做这个:

方法 1 使用ss 不排序(不推荐)

第 1 步 - 获取 PID

ss -lntp | grep node | cut -d "=" -f2 - | cut -d "," -f 1 - > pids.txt

解释:

  1. ss -lntp 将打印套接字统计列表。
  2. grep node 将只打印节点 process/sockets.
  3. cut -d "=" -f2 - 将使用 = 作为分隔符剪切命令 #2 的输出并获得第二列 (-f2)。示例结果是 26000,fd
  4. cut -d "," -f 1 - 从命令 #3 中删除 ,fd 并将使用 , 作为分隔符剪切步骤 #2 的输出并获取第一列 (-f1 ).示例结果是 26000
  5. > pids.txt 将输出保存到 pids.txt 文件。

第 2 步 - 获取端口

ss -lntp | grep node | awk '{ print  }' > ports.txt

解释:

  1. ss -lntp 将打印套接字统计列表。
  2. grep node 将只打印节点 process/sockets.
  3. awk '{ print }' 将打印第四列,即包含 0.0.0.0:6031
  4. 的那一列
  5. > ports.txt 将输出保存到 ports.txt 文件。

步骤 3 - 从 PID 获取路径

ps -eLf | grep -Fwf pids.txt   | awk '!x[]++' | awk '{ print  }' > proc.txt

解释:

  1. ps -eLf 将打印进程状态列表。
  2. grep -Fwf pids.txt 将仅打印 pids.txt 中的 pid/process 个数字。(但它仍然有很多重复的主进程)
  3. awk '!x[]++' 将只过滤唯一的 pid 号。 pid 号在第二列,所以使用 </code></li> <li><code>awk '{ print }' 将只打印第 11 列。路径在第 11 列,所以使用 </code></li> <li><code>> proc.txt 将输出保存到 proc.txt 文件。

步骤 4 - 显示列表合并路径、端​​口和进程

paste -d" " proc.txt ports.txt pids.txt

示例输出(与我的问题相同的服务器)

root@lamp-s-4vcpu-8gb-sgp1-01:~# paste -d" " proc.txt ports.txt pids.txt
/var/www/******/dist/index.js 127.0.0.1:6030 26000
/var/www/api-****-id/dist/index.js 0.0.0.0:6031 26006
/var/www/api.****.com/dist/index.js 127.0.0.1:6039 26013
/var/www/***-nuxt/node_modules/.bin/nuxt 127.0.0.1:3001 29886

可以简单到一行:

ss -lntp | grep node | cut -d "=" -f 2 - | cut -d "," -f 1 - > pids.txt && ss -lntp | grep node | awk '{ print  }' > ports.txt && ps -eLf | grep -Fwf pids.txt   | awk '!x[]++' | awk '{ print  }' > proc.txt && paste -d" " proc.txt ports.txt pids.txt

也许上面的命令可以改进,因为我今天刚学了一些命令,比如sssortpastecutawkps

如果来自 ss 命令的 pids 列表是升序的,上述方法工作正常。但是在另一台服务器上尝试之后,上述命令有时无法正常工作,因为可以交换 pids 和路径位置。所以我试图找到另一种使用如下排序的方法:.

方法 2 使用 netstat 和 PID 排序(推荐)

第 1 步 - 获取 PID

netstat -lntp | grep node | sort -k 7,7 |  awk '{ print  }' | cut -d "/" -f 1 - > pids.txt

第 2 步 - 获取端口

netstat -lntp | grep node | sort -k 7,7 |  awk '{ print  }' > ports.txt

步骤 3 - 从 PID 获取路径

ps -eLf | grep -Fwf pids.txt   | awk '!x[]++' |  sort -k 2,2 | awk '{ print  }' > proc.txt

步骤 4 - 显示列表合并路径、端​​口和进程

paste -d" " proc.txt ports.txt pids.txt