0

There are 3 processes. 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.

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

Please post any solution.

4

5 回答 5

5

Try

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

wc -l counts lines

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

于 2012-07-06T10:48:34.310 回答
2

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

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

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
于 2012-07-06T10:49:28.253 回答
0

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

于 2014-11-08T14:25:49.573 回答
0

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


解决方案 1

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

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

  • wc -l = 统计所有结果

  • 输出 = 3

解决方案 2

使用特定标识符命名脚本并对其进行 grep,例如:

script_example_ident .pl _

ps ax |grep "ident" |wc -l

于 2018-11-14T14:43:05.167 回答
-1

如果通过运行您的意思是“排队等待执行”:

ps ax -o stat,args |grep '^R'|wc -l
于 2012-07-06T10:50:54.703 回答