如何在不使用 grep 的情况下按名称搜索进程?

May*_*hux 76 command-line process

为了搜索过程中,你可以使用psgrep

例如搜索firefox

ps aux | grep firefox
Run Code Online (Sandbox Code Playgroud)

如何在不使用的情况下获得相同的答案grep

mur*_*uru 85

pgrep命令及其兄弟pkill,正是为此目的而存在的:

  • pgrep firefox 将列出所有命令匹配的进程 firefox
  • pgrep -f firefox 将列出整个命令行匹配的所有进程 firefox
  • pgrep -x firefox 将列出命令完全匹配的所有进程 firefox
  • ... 等等。

并且自然而然地pgrep将自己排除在比赛之外,因此不需要grep与之相关的任何仪式ps | grep


用于此的另一组工具是pidofkillall命令。这些都不如灵活pgreppkill

  • pidof firefox 将列出其命令为 firefox


May*_*hux 32

ps -fC process-name
Run Code Online (Sandbox Code Playgroud)

例子:

ps -fC firefox
Run Code Online (Sandbox Code Playgroud)

man ps

  -C cmdlist      Select by command name.
                       This selects the processes whose executable name is
                       given in cmdlist.


 -f              Do full-format listing. This option can be combined
                       with many other UNIX-style options to add additional
                       columns. It also causes the command arguments to be
                       printed. When used with -L, the NLWP (number of
                       threads) and LWP (thread ID) columns will be added. See
                       the c option, the format keyword args, and the format
                       keyword comm.
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案,但不幸的是不适用于 OSX。BSD `ps -C` 标志的行为完全不同——“改变 CPU 百分比的计算方式” (2认同)

小智 5

一个很酷的技巧

$ps -ejH
Run Code Online (Sandbox Code Playgroud)

您将获得所有带有名称的进程

exmple:
1747   568   568 ?        00:00:00   colord
1833  1832  1832 ?        00:00:00   gnome-keyring-d
2263   568   568 ?        00:00:00   udisksd
2311  2311  2311 ?        00:00:00   cupsd
2315  2315  2311 ?        00:00:00     dbus
Run Code Online (Sandbox Code Playgroud)

重定向或将输出复制到文件中,然后打开nano,按Ctrl+W 即可搜索所需的名称。