如何查找具有 ___ 状态的 Solaris 进程

发布于 2024-09-12 19:28:34 字数 630 浏览 8 评论 0原文

我制作了以下脚本,它搜索某些进程,显示每个进程的使用 pflags,并在找到带有“pause”一词的进程时停止:

!cat find_pause
#!/usr/bin/perl -W

use warnings;
use strict;

if (open(WCF,
         "ps -ef | grep '/transfile' | cut -c10-15 | xargs -n1 pflags 2>&1 |"
   )) {
    while (<WCF>) {
       next if ($_ =~ /cannot/);
       print $_;
       last if ($_ =~ /pause/);
    }
    close(WCF);
}

它可以工作,但我想知道是否有更好的方法来做到这一点。

Update

pause 是一个低级系统调用。像readnanosleepwaitid等。

通过这个脚本,我想找到陷入pause的进程> 打电话。我们正在尝试在我们的系统中找到一个错误,我们认为它可能与此有关。

I made the following script which searches for certain processes, displays uses pflags for each one, and stops when it finds one with the word "pause":

!cat find_pause
#!/usr/bin/perl -W

use warnings;
use strict;

if (open(WCF,
         "ps -ef | grep '/transfile' | cut -c10-15 | xargs -n1 pflags 2>&1 |"
   )) {
    while (<WCF>) {
       next if ($_ =~ /cannot/);
       print $_;
       last if ($_ =~ /pause/);
    }
    close(WCF);
}

It works, but I wonder if there is a better way to do this.

Update

pause is a low-level system call. Like read, nanosleep, waitid, etc.

With this script I want to find processes that are stuck in the pause call. We are trying to find a bug in our system, and we think it might be related to this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

国产ˉ祖宗 2024-09-19 19:28:34

我不知道在这种情况下您认为“更好的方法”是什么,但我可以为您已有的方法提供一些技术指导:

  • grep '/[t]ransfile'

    针对 ps 输出的 grep 通常会面临与 grep 进程本身匹配的风险,而这几乎是我们所不希望的。防止这种情况的一种简单方法是在 grep 模式参数中引入一个成员的字符类。

  • awk '/\/[t]ransfile/{ print $2 }'

    grep + cut,即模式匹配后的字段提取,对于单个 awk 命令来说是一项简单的任务。

  • 不要引用$_

    更严格、更惯用的 Perl 将省略 $_ 的显式使用。尝试 next if /cannot/ 等。

  • 打开(我的$wcf,...)

    请使用词法文件句柄,否则你会被那些年纪大到记得我们什么时候不能使用它们的人责骂。 :)

I don't know what you'd consider a "better way" in this case, but I can offer some technique guidance for the approach you already have:

  • grep '/[t]ransfile'

    A grep against ps output often runs the risk of matching the grep process itself, which is almost never desired. An easy protection against this is simply to introduce a character class of one member in the grep pattern argument.

  • awk '/\/[t]ransfile/{ print $2 }'

    grep + cut, that is, field extraction following a pattern match, is an easy task for a single awk command.

  • Don't refer to $_

    Tighter, more idiomatic perl would omit explicit use of $_. Try next if /cannot/ and the like.

  • open(my $wcf, ...)

    Please use lexical filehandles, otherwise you'll be chided by those old enough to remember when we couldn't use them. :)

月亮是我掰弯的 2024-09-19 19:28:34

对此有两种可能的改进,具体取决于:

  1. 您是否确实需要打印pflags命令的精确输出或其中的一些信息(例如PID和标志列表?)

  2. pflags 输出中的“暂停”是什么意思?它不在“proc”或“pflags”手册页中,并且所有实际标志都是大写的。根据其含义,它可能会在“/proc”的本机 Perl 实现中找到 - Proc::processTable::Process

    例如,该 Process 对象包含所有标志 (位向量)和进程状态(我怀疑“暂停”可能是进程状态)。


如果这些问题的答案是“Proc::processTable: :Process 包含足够的信息来满足我的需要”,那么更好的解决方案是使用它:

#!/usr/bin/perl -W
use warnings;
use strict;

use Proc::ProcessTable;

my $t = new Proc::ProcessTable;
foreach $p ( @{$t->table} ) {
   my $flags = $p->pid; # This is an integer containing bit vector.
   # Somehow process $flags or $p->status to find "if the process is paused"
   print "$flags\n";
   last if paused($p); # No clue how to do that without more info from you
   # May be : last if $p->status =~ /paused/;
}

但是,如果本机 Perl 进程没有足够的信息供您使用(不太可能但可能),或者如果您实际上想要打印由于某种原因,精确的 pflags 输出原样,最好的优化是本地构建 pflags 的 PID 列表 - 虽然不是那么大的胜利,但你仍然会失去一堆额外的分叉进程。像这样的事情:

#!/usr/bin/perl -W
use warnings;
use strict;

use Proc::ProcessTable;

my $t = new Proc::ProcessTable;
my $pids = join " ", map { $_->pid } @{$t->table};
if (open(WCF, "pflags 2>&1 $pids|")) {
    while (<WCF>) {
       next if ($_ =~ /cannot/);
       print $_;
       last if ($_ =~ /pause/);
    }
    close(WCF);
}

There are two possible improvements to this, depending on:

  1. Do you actually require to print exact output of pflags command or some info from it (e.g. list of PIDs and flags?)

  2. What does "pause" in pflags output mean? It's nowhere in "proc" or "pflags" man-pages and all the actual flags are upper case. Depending on its meaning, it might be found in native Perl implementation of "/proc" - Proc::processTable::Process.

    For example, that Process object contains all the flags (in a bit vector) and process status (my suspicion is that "pause" might be a process status).


If the answers to those questions are "Proc::processTable::Process contains enough info for my needs", then a better solution is to use that:

#!/usr/bin/perl -W
use warnings;
use strict;

use Proc::ProcessTable;

my $t = new Proc::ProcessTable;
foreach $p ( @{$t->table} ) {
   my $flags = $p->pid; # This is an integer containing bit vector.
   # Somehow process $flags or $p->status to find "if the process is paused"
   print "$flags\n";
   last if paused($p); # No clue how to do that without more info from you
   # May be : last if $p->status =~ /paused/;
}

However, if the native Perl process does not have enough info for you (unlikely but possible), OR if you acually desire to print exact pflags output as-is for some reason, the best optimization is to construct a list of PIDs for pflags natively - not as big of a win but you still lose a bunch of extra forked off processes. Something like this:

#!/usr/bin/perl -W
use warnings;
use strict;

use Proc::ProcessTable;

my $t = new Proc::ProcessTable;
my $pids = join " ", map { $_->pid } @{$t->table};
if (open(WCF, "pflags 2>&1 $pids|")) {
    while (<WCF>) {
       next if ($_ =~ /cannot/);
       print $_;
       last if ($_ =~ /pause/);
    }
    close(WCF);
}

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