我如何获得我的主进程(而不是它的一个工人)的 PI​​D?

Dav*_*ave 3 process ps centos shell-script

我正在使用 CentOS 7。我正在尝试编写一个脚本来启动和停止 puma 进程,但如果 taht 是正确的术语,我无法弄清楚如何获取“主”PID。在下面的命令中

[rails@server myproject_production]$ ps aux | grep puma
rails    15767  0.0  1.2 437904 13612 ?        Sl   17:20   0:00 puma 3.11.4 (tcp://0.0.0.0:3000,unix:///home/rails/myproject_production/shared/sockets/puma.sock) [myproject_production]
rails    15779  0.6  7.6 1061248 80688 ?       Sl   17:20   0:05 puma: cluster worker 1: 15767 [myproject_production]
rails    15781  0.6  7.7 1061248 80876 ?       Sl   17:20   0:05 puma: cluster worker 2: 15767 [myproject_production]
rails    15785  0.6  7.4 1061964 78488 ?       Sl   17:20   0:05 puma: cluster worker 3: 15767 [myproject_production]
rails    15880  0.7  7.4 1059612 78592 ?       Sl   17:22   0:05 puma: cluster worker 0: 15767 [myproject_production]
rails    17106  0.0  0.1 112612  1064 pts/0    S+   17:33   0:00 grep --color=auto puma
Run Code Online (Sandbox Code Playgroud)

主 PID 是“15767”。如果我杀死所有其他美洲狮进程将死亡。如何编写命令以将 taht 写入脚本变量?

Gil*_*il' 5

使用pgrep而不是过滤来自ps. 我认为在您的情况下,pgrep -f '^([^ ]*/)?puma '将匹配正确的流程,但请进行一些试验以确保您获得所需的东西而不是更多。

一旦您对pgrep找到要杀死的进程感到满意,请替换pgreppkill.