简体   繁体   English

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

[英]Linux Command to find out "count" of running process?

There are 3 processes.有3个过程。 I have to find the "count" of running process in current directory.我必须在当前目录中找到正在运行的进程的“计数”。

# 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.请发布任何解决方案。

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关于如何显示流程的完整路径,请看这里

This really isn't the right place for this (there's a linux part of stackexchange).这真的不是正确的地方(stackexchange 有一个 linux 部分)。 But you can use wc to count lines in any output, so pipe your command to it like this: ps -eaf | grep perl | wc -l但是您可以使用wc来计算任何输出中的行数,因此可以像这样将您的命令传递给它: ps -eaf | grep perl | wc -l ps -eaf | grep perl | wc -l ps -eaf | grep perl | wc -l . ps -eaf | grep perl | wc -l By the way I'd also recommend making the grep command 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).顺便说一句,我还建议通过执行grep [p]erl使grep命令与自身不匹配(将 [] 放在任何一个字符周围仍将仅匹配“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 $8}' | grep "^.*/*${file##*/}$" | wc -l)
    echo "$count processes found."
    total=$(($total + $count))
done
echo $total

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

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 = Processlist ps ax = 进程列表
  • egrep = gets all processes from the list that start with the specified path. egrep = 从列表中获取以指定路径开头的所有进程。 You need to start your Perl scripts with Path/Script.pl and not like./script.您需要使用 Path/Script.pl 而不是 ./script 来启动您的 Perl 脚本。 ^ = Multiline - You can also use normal grep "/your/path/to/script" ^ = 多行 - 你也可以使用普通的 grep "/your/path/to/script"

  • wc -l = Count all results wc -l = 计算所有结果

  • Output = 3输出 = 3

Solution 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

If by running you mean "queued for execution":如果通过运行你的意思是“排队等待执行”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM