通过命令行在 Linux 中查找进程数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3058137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:08:26  来源:igfitidea点击:

Finding process count in Linux via command line

linuxcommand-lineprocesscount

提问by Traker

I was looking for the best way to find the number of running processes with the same name via the command line in Linux. For example if I wanted to find the number of bash processes running and get "5". Currently I have a script that does a 'pidof ' and then does a count on the tokenized string. This works fine but I was wondering if there was a better way that can be done entirely via the command line. Thanks in advance for your help.

我一直在寻找通过 Linux 中的命令行查找具有相同名称的正在运行的进程数量的最佳方法。例如,如果我想找到正在运行的 bash 进程的数量并获得“5”。目前我有一个脚本,它执行 'pidof ',然后对标记化的字符串进行计数。这工作正常,但我想知道是否有更好的方法可以完全通过命令行完成。在此先感谢您的帮助。

采纳答案by David Z

On systems that have pgrepavailable, the -coption returns a count of the number of processes that match the given name

pgrep可用的系统上,该-c选项返回与给定名称匹配的进程数的计数

pgrep -c command_name

Note that this is a grep-style match, not an exact match, so e.g. pgrep shwill also match bashprocesses. If you want an exact match, also use the -xoption.

请注意,这是一个grep-style 匹配,而不是完全匹配,因此 egpgrep sh也将匹配bash进程。如果您想要完全匹配,也可以使用该-x选项。

If pgrepis not available, you can use psand wc.

如果pgrep不可用,您可以使用pswc

ps -C command_name --no-headers | wc -l

The -Coption to pstakes command_nameas an argument, and the program prints a table of information about processes whose executable name matches the given command name. This is an exact match, not grep-style. The --no-headersoption suppresses the headers of the table, which are normally printed as the first line. With --no-headers, you get one line per process matched. Then wc -lcounts and prints the number of lines in its input.

-C选项ps需要command_name作为参数,该程序将打印信息表,谁的可执行文件名给定的命令名称相匹配的过程。这是完全匹配,而不是grep风格。该--no-headers选项禁止表格的标题,通常打印为第一行。使用--no-headers,每个匹配的进程都会得到一行。然后wc -l计算并打印其输入中的行数。

回答by Amardeep AC9MF

result=`ps -Al | grep command-name | wc -l`
echo $result

回答by Alex B

ps -Al | grep -c bash

回答by Sury Soni

Following bash script can be run as a cron job and you can possibly get email if any process forks itself too much.

以下 bash 脚本可以作为 cron 作业运行,如果任何进程分叉太多,您可能会收到电子邮件。

for i in `ps -A -o comm= --sort=+comm | uniq`; 
do 
    if (( `ps -C $i --no-headers | wc -l` > 10 )); then 
        echo `hostname` $i `ps -C $i --no-headers | wc -l` ;
    fi
done

Replace 10 with your number of concern.

将 10 替换为您关注的号码。

TODO: "10" could be passed as command line parameter as well. Also, few system processes can be put into exception list.

TODO:“10”也可以作为命令行参数传递。此外,很少有系统进程可以放入例外列表。

回答by Niall O Shea

Some of the above didn't work for me, but they helped me on my way to this.

上面的一些方法对我不起作用,但它们在我实现这一目标的过程中帮助了我。

ps aux | grep [j]ava -c

For newbies to Linux:

对于 Linux 新手:

ps auxprints all the currently running processes, grepsearches for all processes that match the word java, the []brackets remove the process you just ran so it wont include that as a running process and finally the -coption stands for count.

ps aux打印所有正在运行的进程,grep为匹配词,所有的进程搜索的Java中,[]括号中移除刚才跑得那么它不会有,作为一个正在运行的进程,最后的处理-c选项代表计数。

回答by Bhagya Prasad NR

You can try :

你可以试试 :

ps -ef | grep -cw [p]rocess_name

OR

或者

ps aux | grep -cw [p]rocess_name

For e.g.,:

例如:

ps -ef | grep -cw [i]nit

回答by fraff

List all process names, sort and count

列出所有进程名称,排序和计数

ps --no-headers -A -o comm | sort | uniq -c

You also can list process attached to a tty

您还可以列出附加到 tty 的进程

ps --no-headers a -o comm | sort | uniq -c

You may filter with:

您可以过滤:

ps --no-headers -A -o comm | awk '{ list[] ++ } END { for (i in list) { if (list[i] > 10) printf ("%20s: %s\n", i, list[i]) } }'

回答by BreakBadSP

You can use ps(will show snapshot of processes) with wc(will count number of words, wc -loption will count lines i.e. newline characters). Which is very easy and simple to remember.

您可以使用ps(将显示进程的快照)与wc(将计算单词数,wc -l选项将计算行数,即换行符)。这很容易记住。

ps -e | grep processName | wc -l

ps -e | grep processName | wc -l

This simple command will print number of processes running on current server. If you want to find the number of process running on current server for current user then use -Uoption of ps.

这个简单的命令将打印当前服务器上运行的进程数。如果您想了解当前服务器上运行的当前用户进程的数量,然后使用-U的选项ps

ps -U root | grep processName | wc -l

ps -U root | grep processName | wc -l

change root with username.

用用户名更改root。

But as mentioned in lot of other answers you can also use ps -e | grep -c process_namewhich is more elegant way.

但是正如许多其他答案中提到的,您也可以使用ps -e | grep -c process_name哪种更优雅的方式。

回答by spoorthi vaidya

ps aux | wc -l

ps aux | wc -l

This command shows number of processes running on the system by all the users.

此命令显示所有用户在系统上运行的进程数。

For a specific user you can use the following command:

对于特定用户,您可以使用以下命令:

ps -u <username> | wc -l

ps -u <username> | wc -l

replace with the actual username before running :)

在运行之前替换为实际用户名:)