Linux命令找出正在运行的进程的“计数”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11360735/
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-06 13:43:48  来源:igfitidea点击:

Linux Command to find out "count" of running process?

linux

提问by Anand

There are 3 processes. I have to find the "count" of running process in current directory.

有3个进程。我必须在当前目录中找到正在运行的进程的“计数”。

# named ../sample

And:

和:

[root@sp3 sample]# ps -eaf|grep perl
root     14104     1 58 08:39 ?        03:31:34 perl example1.pl
root     17441     1 41 09:09 ?        02:17:24 perl example2.pl
root     24543     1  0 Jul05 ?        00:00:00 perl sample.pl
[root@sp3 sample]#

The result I have to return is 3.

我必须返回的结果是 3。

I have currently in sample directory and I have to count the number of process in same directory

我目前在示例目录中,我必须计算同一目录中的进程数

Please post any solution.

请发布任何解决方案。

回答by BigMike

Try

尝试

LOCAL_PWD = pwd
ps -auxeaf| grep $LOCAL_PWD| wc -l

wc -l counts lines

wc -l 计算行数

Regarding how to show full path of processes please take a look at here

关于如何显示进程的完整路径,请看这里

回答by Thor84no

This really isn't the right place for this (there's a linux part of stackexchange). But you can use wcto count lines in any output, so pipe your command to it like this: ps -eaf | grep perl | wc -l. By the way I'd also recommend making the grepcommand not match itself by doing grep [p]erl(putting []'s around any one character will still match only 'perl', but the grep command no longer has 'perl' in it).

这真的不是合适的地方(stackexchange 有一个 linux 部分)。但是你可以用wc计算在任何输出线,让您的管道命令,它是这样的:ps -eaf | grep perl | wc -l。顺便说一句,我还建议grep通过执行以下操作使命令与自身不匹配grep [p]erl(将 [] 放在任何一个字符周围仍将仅匹配 'perl',但 grep 命令中不再包含 'perl')。

In case it's of use to you I've put together a script like I mentioned in the comment below.

如果它对您有用,我已经整理了一个脚本,就像我在下面的评论中提到的那样。

total=0
for file in $(find -executable -type f) ; do
    echo "Checking $file:"
    count=$(ps -ef | awk '{print }' | grep "^.*/*${file##*/}$" | wc -l)
    echo "$count processes found."
    total=$(($total + $count))
done
echo $total

回答by fork0

If by running you mean "queued for execution":

如果运行是指“排队等待执行”:

ps ax -o stat,args |grep '^R'|wc -l

回答by Eliran Ben-Zikri

Simple: ps -eaf | grep perl | grep -v grep | wc -l

简单: ps -eaf | grep perl | grep -v grep | wc -l

回答by Darius D

I know the topic is old, but there is no solution for this Problem.

我知道这个话题很旧,但是这个问题没有解决方案。



Solution 1

解决方案1

ps ax |egrep "^/your/path/to/Script" |wc -l

ps ax |egrep "^/your/path/to/Script" |wc -l

  • ps ax = Processlist
  • egrep = gets all processes from the list that start with the specified path. You need to start your Perl scripts with Path/Script.pl and not like ./script. ^ = Multiline - You can also use normal grep "/your/path/to/script"

  • wc -l = Count all results

  • Output = 3
  • ps ax = 进程列表
  • egrep = 从列表中获取以指定路径开头的所有进程。您需要使用 Path/Script.pl 而不是 ./script 来启动您的 Perl 脚本。^ = 多行 - 您也可以使用普通的grep "/your/path/to/script"

  • wc -l = 计算所有结果

  • 输出 = 3


Solution 2

解决方案2

Name you scripts with a specific identifier and grep for it like:

使用特定标识符为您的脚本命名并为其使用 grep,例如:

script_example_ident.pl

script_example_ ident.pl

ps ax |grep "ident" |wc -l

ps ax |grep "ident" |wc -l