通过命令行在Linux中查找进程数
我一直在寻找通过 Linux 中的命令行查找正在运行的同名进程数量的最佳方法。例如,如果我想查找正在运行的 bash 进程的数量并得到“5”。目前,我有一个脚本执行“pidof”,然后对标记化字符串进行计数。这工作正常,但我想知道是否有更好的方法可以完全通过命令行完成。预先感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在具有
pgrep
可用的 Linux 系统(也许还有一些非 Linux 系统)上,-c
/--count
选项返回与给定名称匹配的进程数的计数:如果
pgrep
不可用,您可以使用ps
和wc
。同样,这可能在某种程度上取决于系统,但在典型的 Linux 系统上,以下命令将为您提供进程计数:ps
的-C
选项采用command_name
作为参数,程序打印有关其可执行名称与给定命令名称匹配的进程的信息表。-e
使其搜索所有用户拥有的进程,而不仅仅是调用用户。--no-headers
选项抑制表的标题,这些标题通常打印为第一行。使用--no-headers
,每个进程匹配一行。然后 wc -l 计算并打印其输入中的行数。这些命令之间有一些关键区别:
pgrep
使用grep
样式匹配,因此例如pgrep sh
也将匹配bash< /代码> 进程。如果您想要精确匹配,还可以使用
-x
/--exact
选项。另一方面,ps -C
使用精确匹配,而不是grep
风格。ps
将为自身和/或它通过管道传输到的任何其他进程输出一行,如果它们符合您给出的进程选择标准。所以 ps -C ps --no-headers | wc -l 和 ps -C wc --no-headers | wc -l 和 ps -C wc --no-headers | wc -l 将始终输出至少 1,因为它们正在计算自己。另一方面,pgrep
排除自身。因此,除非有其他单独的 pgrep 进程正在运行,否则 pgrep --count pgrep 将输出 0。 (通常,pgrep
行为就是您想要的,这就是为什么我通常建议使用pgrep
(如果可用)。)以下是一些变体的摘要:
pgrep -c command_name
pgrep -U "$UID" -c command_name
ps -C command_name --无标题 | wc -l
还有很多可能性,这里无法一一列出。如果您需要不同的行为,请检查
pgrep
和ps
的手册页,您可能会找到实现它的选项。On Linux systems (and perhaps some non-Linux systems as well) that have
pgrep
available, the-c
/--count
option returns a count of the number of processes that match the given name:If
pgrep
is not available, you may be able to useps
andwc
. Again this may be somewhat system-dependent, but on typical Linux systems the following command will get you a count of processes:The
-C
option tops
takescommand_name
as an argument, and the program prints a table of information about processes whose executable name matches the given command name.-e
causes it to search processes owned by all users, not just the calling user. 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. Thenwc -l
counts and prints the number of lines in its input.There are a few key differences between these commands:
pgrep
usesgrep
-style matching, so e.g.pgrep sh
will also matchbash
processes. If you want an exact match, also use the-x
/--exact
option. On the other hand,ps -C
uses exact matching, notgrep
-style.ps
will output a row for itself and/or any other process it's being piped to, if those match the process selection criteria you gave. Sops -C ps --no-headers | wc -l
andps -C wc --no-headers | wc -l
will always output at least 1, because they are counting themselves. On the other hand,pgrep
excludes itself. Sopgrep --count pgrep
will output 0 unless there are other separatepgrep
processes running. (Typically thepgrep
behavior is what you want, which is why I generally recommend usingpgrep
if it's available.)Here's a summary of a few variations:
pgrep -x -c command_name
ps -C command_name -e --no-headers | wc -l
pgrep -c command_name
pgrep -U "$UID" -c command_name
ps -C command_name --no-headers | wc -l
There are many more possibilities, too many to list here. Check the man pages for
pgrep
andps
if you need different behavior, and you might find an option that implements it.您可以尝试:
或
例如:
You can try :
OR
For e.g.,:
上面的一些方法对我不起作用,但它们帮助我实现了这一目标。
对于 Linux 新手:
ps aux
打印所有当前正在运行的进程,grep
搜索与单词 java 匹配的所有进程,[]
括号删除您刚刚运行的进程,因此它不会将其包含为正在运行的进程,最后-c
选项代表计数。Some of the above didn't work for me, but they helped me on my way to this.
For newbies to 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.列出所有进程名称、排序和计数
您还可以列出附加到 tty 的进程
您可以使用以下命令进行过滤:
List all process names, sort and count
You also can list process attached to a tty
You may filter with:
此命令显示所有用户在系统上运行的进程数。
对于特定用户,您可以使用以下命令:
在运行之前替换为实际用户名:)
This command shows number of processes running on the system by all the users.
For a specific user you can use the following command:
replace with the actual username before running :)
以下 bash 脚本可以作为 cron 作业运行,如果任何进程分叉过多,您可能会收到电子邮件。
将 10 替换为您关注的号码。
TODO:“10”也可以作为命令行参数传递。此外,很少有系统进程可以放入例外列表。
Following bash script can be run as a cron job and you can possibly get email if any process forks itself too much.
Replace 10 with your number of concern.
TODO: "10" could be passed as command line parameter as well. Also, few system processes can be put into exception list.
您可以将
ps
(将显示进程快照)与wc
(将计算单词数,wc -l
选项将计算行数,即换行符)一起使用人物)。这非常容易记住。
ps -e | ps -e | ps -e | ps -e | ps -e | ps -e grep 进程名称 | wc -l
这个简单的命令将打印当前服务器上运行的进程数。
如果您想查找当前用户在当前服务器上运行的进程数,请使用
ps
的-U
选项。ps -U 根 | grep 进程名称 | wc -l
用用户名更改 root。
但正如许多其他答案中提到的,您也可以使用 ps -e | grep -c process_name 这是更优雅的方式。
You can use
ps
(will show snapshot of processes) withwc
(will count number of words,wc -l
option will count lines i.e. newline characters).Which is very easy and simple to remember.
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 ofps
.ps -U root | grep processName | wc -l
change root with username.
But as mentioned in lot of other answers you can also use
ps -e | grep -c process_name
which is more elegant way.这里“CAP”是我的 Process_Names 中的单词。
此命令输出 = 进程数 + 1
这就是为什么当我们运行此命令时,我们的系统读取“ps -awef | grep CAP | wc -l ”也是一个进程。
所以是的,我们真正的答案是(进程数)=命令输出 - 1
注意:这些进程只是那些包含“CAP”名称的进程
Here "CAP" is the word which is in the my Process_Names.
This command output = Number of Processes + 1
This is why When we are running this command , our system read thats "ps -awef | grep CAP | wc -l " is also a process.
So yes our real answer is (Number of Processes) = Command Output - 1
Note : These processes are only those processes who include the name of "CAP"