有没有办法让pgrep给我关于ps每个进程的所有信息? 我知道我可以通过greppipeps ,但这是很多打字,它也给我我不想要的grep过程本身。
pgrep的输出选项非常有限。 你几乎可以肯定需要通过ps发回来获取重要的信息。 您可以通过在~/.bashrc使用bashfunction来自动执行此操作。
function ppgrep() { pgrep "$@" | xargs --no-run-if-empty ps fp; }
然后用命令调用。
ppgrep <pattern>
用xargs pgrep和ps结合起来!
pgrep <your pgrep-criteria> | xargs ps <your ps options> -p
例如尝试
pgrep -u user | xargs ps -f -p
获取user的完整进程列表。
保持第一行的列名是很好的。 grep总是删除列名。
以下仅给出PID +完整的命令行。 对于“所有信息ps ”,请参阅其他答案…
大多数linux使用procps-ng 。 自3.3.4(2012年发布)以来, pgrep -a (– --list-full )显示了完整的命令行。
注意:默认情况下,pgrep只能匹配您对可执行文件名称的模式。 如果你想匹配完整的命令行(如grep ps),添加-f (– --full )选项。
在较旧版本(包括原始procps项目)中, -l选项显示信息,但其行为各不相同:
pgrep -fl与完整的命令行匹配,显示完整的命令行。 pgrep -l只匹配可执行文件的名称,并只显示可执行文件的名称。 不知道什么代码* BSD使用,但他们的手册页文件旧的-fl行为。
不幸的是,你甚至不能使用-fl – 在最近的procps-ng中, -f (–list --list-name )总是只打印可执行文件的名字。
使用-v选项来grep – 它返回所有内容但是请求的模式。
ps -ef | grep <process> | grep -v grep
在OSX上(通过推理,在BSD上) -l与-f结合将显示完整的命令( -i增加了不区分大小写):
$ pgrep -fil ssh OUTPUT: 33770 ssh: [email protected] [mux] t
手册页 :
-l Long output. For pgrep, print the process name in addition to the process ID for each matching process. If used in conjunction with -f, print the process ID and the full argument list for each matching process. For pkill, dis- play the kill command used for each process killed.
对于GNU版本的pgrep ,不支持-i (不区分大小写),长输出是用-a来实现的。
$ pgrep -af apache2 OUTPUT: 1748 /usr/sbin/apache2 -k start
手册页 :
-a, --list-full List the full command line as well as the process ID. (pgrep only.)
我不认为有,你可以得到最多的信息是通过使用pgrep的-l选项的名称和进程ID。
ps支持各种格式化选项,所以我只想为你想保存的input做一个别名。 从输出中排除grep进程的简单方法是将grep -v grep的其他pipe道包含在内,以排除任何grep进程。
为了消除grep过程,可以使用括号作为模式的一部分:
ps -ef | grep '[t]ty'
你可以用ps和pgrep来做到这一点:
ps -fp $(pgrep -d, tty)
这将帮助你我猜:
ps auxww