PS:清洁方式只得到父进程? [英] ps: Clean way to only get parent processes?

查看:170
本文介绍了PS:清洁方式只得到父进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 PS EF PS RF 很多。

下面是一个示例输出 PS RF

  PID TTY      STAT   TIME COMMAND
 3476 pts/0    S      0:00 su ...
 3477 pts/0    S      0:02  \_ bash
 8062 pts/0    T      1:16      \_ emacs -nw ...
15733 pts/0    R+     0:00      \_ ps xf
15237 ?        S      0:00 uwsgi ...
15293 ?        S      0:00  \_ uwsgi ...
15294 ?        S      0:00  \_ uwsgi ...

今天,我需要找回仅在主uwsgi的过程在脚本(所以我想只有15237,但并不15293 15294也没有)。

And today I needed to retrieve only the master process of uwsgi in a script (so I want only 15237 but not 15293 nor 15294).

截至今日,我尝试了一些 PS射频| grep的-v'\\\\ _ ...但我想一个更清洁的方式

As of today, I tried some ps rf | grep -v ' \\_ '... but I would like a cleaner way.

我也来翻过另一个解决方案从unix.com的论坛:

I also came accross another solution from unix.com's forums:

ps xf | sed '1d' | while read pid tty stat time command ; do [ -n "$(echo $command | egrep '^uwsgi')" ] && echo $pid ; done

但仍然是一个很多管道丑陋的招数

难道真的没有 PS 选项或清洁技巧(也许使用的 AWK )来实现这一目标?

Is there really no ps option or cleaner tricks (maybe using awk) to accomplish that?

推荐答案

在他的答案的评论与净@ codeR讨论后,他用一个漂亮的窍门:D结果
PS 使用˚F总是会得到在上面父这是伟大的。

After discussing with @netcoder on his answer's comments he used a nice trick :D
Using f on ps will always get the parent on top which is great.

这应该只是工作:

$ ps hf -opid -C <process> | awk '{ print $1; exit }'

我提上的评论,这将返回 PID 只是一个过程。

我会去的:

ps rf -opid,cmd -C <process-name> | awk '$2 !~ /^[|\\]/ { print $1 }'

这是:


  • 列表运行processses 研究(或电子如果你想要的一切)

  • 与父母/子女沿图˚F

  • 仅输出PID和命令名称 -opid,CMD

  • 只为给定的过程 -C&lt;方法&GT;

  • list running processses r (or e if you want everything)
  • along with parent/children graph f
  • output only the pid and command name -opid,cmd
  • only for the given process -C <process>

然后


  • 如果第二场 - 这是命令( -opid,CMD ) - 不以 \\ 或 | 那么它是一个父进程,所以输出第1场 - 这是PID

  • if the 2nd field - which is the command (-opid,cmd) - does not start with a \ or | then it is a parent process, so print the 1st field - which is the pid.

简单的测试:

$ ps f -opid,cmd -Cchromium
  PID CMD
 2800 /usr/lib/chromium/chromium --type=zygote --enable-seccomp-sandbox
 2803  \_ /usr/lib/chromium/chromium --type=zygote --enable-seccomp-sandbox
 2899      \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/ConnnectB
 2906      |   \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/Connn
 [  ... snip ... ]
 2861      \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/ConnnectB
 2863          \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/Connn
 2794 /usr/lib/chromium/chromium --enable-seccomp-sandbox --memory-model=low --purge-memory-button --disk-cache-dir=/tmp/chromium
 2796  \_ /usr/lib/chromium/chromium --enable-seccomp-sandbox --memory-model=low --purge-memory-button --disk-cache-dir=/tmp/chromium
 3918  \_ /usr/lib/chromium/chromium --type=gpu-process --channel=2794.45.1891443837 --gpu-vendor-id=0x10de --gpu-device-id=0x0611 --gpu-driver-version -
25308  \_ [chromium] <defunct>
31932  \_ /usr/lib/chromium/chromium --type=plugin --plugin-path=/usr/lib/mozilla/plugins/libflashplayer.so --lang=en-US --channel=2794.1330.1990362572


$ ps f -opid,cmd -Cchromium | awk '$2 !~ /^[|\\]/ { print $1 }'
PID
2800
2794

$ # also supressing the header of ps (top line 'PID') -- add 'h' to ps
$ ps hf -opid,cmd -Cchromium | awk '$2 !~ /^[|\\]/ { print $1 }'
2800
2794

这篇关于PS:清洁方式只得到父进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆