尽管 ps | grep 可用,是什么让 pgrep 实用程序如此独特?

尽管 ps | grep 可用,是什么让 pgrep 实用程序如此独特?

有人能解释一下这个实用程序的用途吗?pgrep因为它的功能似乎可以被替换ps | grep?PS 我的老师给我布置了这个问题,并要求我解释为什么如果存在相同的功能,还需要创建一个新的实用程序ps | grep。我怀疑“pgrep”为我们提供了某些ps | grep不存在的东西,但它是什么呢?

答案1

有很多。从man pgrep

-g, --pgroup pgrp,...
      Only  match  processes  in  the  process  group  IDs  listed.   Process  group 0 is
      translated into pgrep's, pkill's, or pidwait's own process group.

-G, --group gid,...
      Only match processes whose real group  ID  is  listed.   Either  the  numerical  or
      symbolical value may be used.
-n, --newest
      Select only the newest (most recently started) of the matching processes.

-o, --oldest
      Select only the oldest (least recently started) of the matching processes.

-O, --older secs
      Select processes older than secs.

-P, --parent ppid,...
      Only match processes whose parent process ID is listed.

-s, --session sid,...
      Only  match  processes  whose  process  session  ID  is  listed.   Session  ID 0 is
      translated into pgrep's, pkill's, or pidwait's own session ID.

-t, --terminal term,...
      Only match processes whose controlling  terminal  is  listed.   The  terminal  name
      should be specified without the "/dev/" prefix.

-u, --euid euid,...
      Only  match  processes  whose effective user ID is listed.  Either the numerical or
      symbolical value may be used.

-U, --uid uid,...
      Only match processes whose real  user  ID  is  listed.   Either  the  numerical  or
      symbolical value may be used.

-v, --inverse
      Negates the matching.  This option is usually used in pgrep's or pidwait's context.
      In pkill's context the short option is disabled to avoid accidental  usage  of  the
      option.

-w, --lightweight
      Shows  all  thread ids instead of pids in pgrep's or pidwait's context.  In pkill's
      context this option is disabled.

-x, --exact
      Only match processes whose names (or command lines  if  -f  is  specified)  exactly
      match the pattern.

-F, --pidfile file
      Read PIDs from file.  This option is more useful for pkill or pidwait than pgrep.

-A, --ignore-ancestors
      Ignore  all ancestors of pgrep, pkill, or pidwait.  For example, this can be useful
      when elevating with sudo or similar tools.

-H, --require-handler
      Only match processes with a userspace signal handler present for the signal  to  be
      sent.

--cgroup name,...
      Match on provided control group (cgroup) v2 name. See cgroups(8)

--ns pid
      Match  processes  that  belong  to  the same namespaces. Required to run as root to
      match processes from other users. See --nslist for how to limit which namespaces to
      match.

--nslist name,...
      Match only the provided namespaces. Available namespaces: ipc, mnt, net, pid, user,
      uts.

尽情制作grep表达式来过滤输出ps aux以匹配精确的这些选项的行为!

相关内容