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

发布于 2024-09-06 14:44:18 字数 151 浏览 4 评论 0原文

我一直在寻找通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

暗地喜欢 2024-09-13 14:44:18

在具有 pgrep 可用的 Linux 系统(也许还有一些非 Linux 系统)上,-c/--count 选项返回与给定名称匹配的进程数的计数:

pgrep --count command_name

如果 pgrep 不可用,您可以使用 pswc。同样,这可能在某种程度上取决于系统,但在典型的 Linux 系统上,以下命令将为您提供进程计数:

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

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(如果可用)。)

以下是一些变体的摘要:

用例pgrepps
Exact匹配pgrep -x -c 命令名ps -C 命令名 -e --no-headers | wc -l
子字符串匹配pgrep -c command_nameN/A
仅当前用户pgrep -U "$UID" -c command_nameps -C command_name --无标题 | wc -l

还有很多可能性,这里无法一一列出。如果您需要不同的行为,请检查 pgrepps 的手册页,您可能会找到实现它的选项。

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:

pgrep --count command_name

If pgrep is not available, you may be able to use ps and wc. Again this may be somewhat system-dependent, but on typical Linux systems the following command will get you a count of processes:

ps -C command_name -e --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. -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. Then wc -l counts and prints the number of lines in its input.

There are a few key differences between these commands:

  • pgrep uses grep-style matching, so e.g. pgrep sh will also match bash processes. If you want an exact match, also use the -x/--exact option. On the other hand, ps -C uses exact matching, not grep-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. So ps -C ps --no-headers | wc -l and ps -C wc --no-headers | wc -l will always output at least 1, because they are counting themselves. On the other hand, pgrep excludes itself. So pgrep --count pgrep will output 0 unless there are other separate pgrep processes running. (Typically the pgrep behavior is what you want, which is why I generally recommend using pgrep if it's available.)

Here's a summary of a few variations:

Use casepgrepps
Exact matchpgrep -x -c command_nameps -C command_name -e --no-headers | wc -l
Substring matchpgrep -c command_nameN/A
Only current userpgrep -U "$UID" -c command_nameps -C command_name --no-headers | wc -l

There are many more possibilities, too many to list here. Check the man pages for pgrep and ps if you need different behavior, and you might find an option that implements it.

素罗衫 2024-09-13 14:44:18
result=`ps -Al | grep command-name | wc -l`
echo $result
result=`ps -Al | grep command-name | wc -l`
echo $result
向日葵 2024-09-13 14:44:18
ps -Al | grep -c bash
ps -Al | grep -c bash
香橙ぽ 2024-09-13 14:44:18

您可以尝试:

ps -ef | grep -cw [p]rocess_name

ps aux | grep -cw [p]rocess_name

例如:

ps -ef | grep -cw [i]nit

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
孤独陪着我 2024-09-13 14:44:18

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

ps aux | grep [j]ava -c

对于 Linux 新手:

ps aux 打印所有当前正在运行的进程,grep 搜索与单词 java 匹配的所有进程,[] 括号删除您刚刚运行的进程,因此它不会将其包含为正在运行的进程,最后 -c 选项代表计数。

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:

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.

尝蛊 2024-09-13 14:44:18

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

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

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

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

您可以使用以下命令进行过滤:

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

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

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

You may filter with:

ps --no-headers -A -o comm | awk '{ list[$1] ++ } END { for (i in list) { if (list[i] > 10) printf ("%20s: %s\n", i, list[i]) } }'
月下伊人醉 2024-09-13 14:44:18

<代码>ps aux | wc -l <​​/code>

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

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

ps -u <用户名>; | wc -l <​​/code>


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

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

replace with the actual username before running :)

俯瞰星空 2024-09-13 14:44:18

以下 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

将 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.

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.

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

记忆消瘦 2024-09-13 14:44:18

您可以将 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) 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 -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.

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.

烟酉 2024-09-13 14:44:18

ps-awef | grep CAP |厕所-l

这里“CAP”是我的 Process_Names 中的单词。

此命令输出 = 进程数 + 1

这就是为什么当我们运行此命令时,我们的系统读取“ps -awef | grep CAP | wc -l ”也是一个进程。

所以是的,我们真正的答案是(进程数)=命令输出 - 1

注意:这些进程只是那些包含“CAP”名称的进程

ps -awef | grep CAP | wc -l

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文