shk*_*der 9 linux bash shell command-line 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 ...
Run Code Online (Sandbox Code Playgroud)
而今天我只需要在脚本中检索uwsgi 的主进程(所以我只需要15237但不需要15293也不需要15294).
截至今天,我尝试了一些ps rf | grep -v ' \\_ '......但我想要一个更清洁的方式.
我也来自unix.com论坛的另一个解决方案:
ps xf | sed '1d' | while read pid tty stat time command ; do [ -n "$(echo $command | egrep '^uwsgi')" ] && echo $pid ; done
Run Code Online (Sandbox Code Playgroud)
但仍然有很多管道和丑陋的技巧.
真的没有ps选择或更干净的技巧(可能使用awk)来实现这一目标吗?
c00*_*ter 10
在与@netcoder讨论他的回答评论后,他使用了一个很好的技巧:D
使用fon ps将始终让父母在顶部,这是伟大的.
这应该工作:
$ ps hf -opid -C <process> | awk '{ print $1; exit }'
Run Code Online (Sandbox Code Playgroud)
正如我在评论中提到的那样,这将只返回pid一个过程.
我会选择:
ps rf -opid,cmd -C <process-name> | awk '$2 !~ /^[|\\]/ { print $1 }'
Run Code Online (Sandbox Code Playgroud)
那是:
r(或者e如果你想要一切)f-opid,cmd -C <process>然后
-opid,cmd) - 不以a开头,\或者|它是父进程,那么打印第一个字段 - 这是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
Run Code Online (Sandbox Code Playgroud)
小智 7
/usr/bin/pgrep -o <process_name>
Run Code Online (Sandbox Code Playgroud)
其中“-o”是匹配进程中最老的(最近最少启动的)
| 归档时间: |
|
| 查看次数: |
19143 次 |
| 最近记录: |