通过命令行在 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
Finding process count in Linux via command line
提问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 pgrep
available, the -c
option 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 sh
will also match bash
processes. If you want an exact match, also use the -x
option.
请注意,这是一个grep
-style 匹配,而不是完全匹配,因此 egpgrep sh
也将匹配bash
进程。如果您想要完全匹配,也可以使用该-x
选项。
If pgrep
is not available, you can use ps
and wc
.
如果pgrep
不可用,您可以使用ps
和wc
。
ps -C command_name --no-headers | wc -l
The -C
option to ps
takes command_name
as 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-headers
option 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 -l
counts 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 aux
prints all the currently running processes, grep
searches 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 -c
option 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 -l
option 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 -U
option 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_name
which 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 :)
在运行之前替换为实际用户名:)