如何查找具有 ___ 状态的 Solaris 进程
我制作了以下脚本,它搜索某些进程,显示每个进程的使用 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
是一个低级系统调用。像read
、nanosleep
、waitid
等。
通过这个脚本,我想找到陷入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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道在这种情况下您认为“更好的方法”是什么,但我可以为您已有的方法提供一些技术指导:
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
againstps
output often runs the risk of matching thegrep
process itself, which is almost never desired. An easy protection against this is simply to introduce a character class of one member in thegrep
pattern argument.awk '/\/[t]ransfile/{ print $2 }'
grep
+cut
, that is, field extraction following a pattern match, is an easy task for a singleawk
command.Don't refer to
$_
Tighter, more idiomatic perl would omit explicit use of
$_
. Trynext 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. :)
对此有两种可能的改进,具体取决于:
您是否确实需要打印
pflags
命令的精确输出或其中的一些信息(例如PID和标志列表?)pflags 输出中的“暂停”是什么意思?它不在“proc”或“pflags”手册页中,并且所有实际标志都是大写的。根据其含义,它可能会在“/proc”的本机 Perl 实现中找到 - Proc::processTable::Process。
例如,该 Process 对象包含所有标志 (位向量)和进程状态(我怀疑“暂停”可能是进程状态)。
如果这些问题的答案是“Proc::processTable: :Process 包含足够的信息来满足我的需要”,那么更好的解决方案是使用它:
但是,如果本机 Perl 进程没有足够的信息供您使用(不太可能但可能),或者如果您实际上想要打印由于某种原因,精确的 pflags 输出原样,最好的优化是本地构建 pflags 的 PID 列表 - 虽然不是那么大的胜利,但你仍然会失去一堆额外的分叉进程。像这样的事情:
There are two possible improvements to this, depending on:
Do you actually require to print exact output of
pflags
command or some info from it (e.g. list of PIDs and flags?)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:
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: