我怎样才能grep PS输出的头文件?
这两个过程组成了我的服务器上运行的应用程序….
root 17123 16727 0 16:25 pts/6 00:00:00 grep GMC root 32017 1 83 May03 ? 6-22:01:17 /scripts/GMC/PNetT-5.1-SP1/PNetTNetServer.bin -tempdir /usr/local/GMC/PNetT-5.1-SP1/tmpData -D
6-22:01:17是不是已经跑了6天了? 我正在努力确定进程已经运行了多长时间…
第二列是进程ID吗? 所以如果我kill 32017它会杀死第二个进程?
ps -ef | egrep "GMC|PID"
根据需要更换“GMC”和ps开关。
示例输出:
root@xxxxx:~$ ps -ef | egrep "disk|PID" UID PID PPID C STIME TTY TIME CMD paremh1 12501 12466 0 18:31 pts/1 00:00:00 egrep disk|PID root 14936 1 0 Apr26 ? 00:02:11 /usr/lib/udisks/udisks-daemon root 14937 14936 0 Apr26 ? 00:00:03 udisks-daemon: not polling any devices
感谢geekosaur,我想用这个命令来满足你的要求,而不是一个单独的命令:
ps -ef | head -1; ps -ef | grep "your-pattern-goes-here"
棘手的是要利用“;” 由shell支持链接命令。
第二列是进程ID; 第四是创build过程(通常是程序启动的时间,但并不总是;考虑execve()和朋友); 第六是CPU消耗的时间。 所以它已经运行了8天,并且使用了将近7天的CPU时间,我认为这很令人担忧。
在相同的调用中获取头部是最棘手的; 我只是做一个单独的ps | head -1 ps | head -1 。 你可能会考虑使用ps自己的select方法,或者像pgrep而不是grep ,而这并不是真的被devise用来传递头文件。
egrep解决scheme是简单而有用的,但是当然你依赖于总是包含'PID'的头部(尽pipe这是一个比较合理的假设),而且相同的string不会在其他地方出现。 我猜测这已经足够满足你的需求了,但是如果有人想要替代品,那就是sed。
Sed让你只是说“打印第一行,然后是任何包含模式的行”。 例如:
ps auxwww | sed -n '1p; /PROCESS_NAME_TO_SEARCH/p;'
添加/sed -n/d; 过滤sed本身:
ps auxwww | sed -n '1p; /sed -n/d; /PROCESS_NAME_TO_SEARCH/p;'
更简单的替代方法: ps -ef | { head -1; grep GMC; } ps -ef | { head -1; grep GMC; }
将数字replace为显示标题的行数。
你可以用pgrep得到pid
pgrep PNetTNetServer
然后用ps和pid
ps u 12345
甚至把两者结合成一个命令
ps u `pgrep PNetTNetServer`
这将显示你想要的行,并包括标题。
我写了一个小的Perl程序,将打印
我经常使用它像ps | 1andre GMC ps | 1andre GMC ,但它也可以采用文件参数(每个文件都提供了自己的标题行,用于匹配来自该文件的行)。
#!/usr/bin/perl # # 1andre <regexp> [<file> ...] # # 1 - first ({1}st) # and - {and} # re - (lines matching) {re}gexp # # If no <files> are given (or "-" is given as a <file>) stdin is # used. # # If any lines from each <file> match the <regexp>, print the # first line from that <file> and all the matching lines from # that <file>. # use strict; use warnings; if(scalar @ARGV < 1) { printf STDERR "usage: %s <regexp> [<file> ...]\n", $0; exit 1; } my $re = shift; my $header; while(<>) { if($. == 1) { $header = $_; } elsif(m/$re/) { if(defined $header) { print $header; undef $header; } print; } } continue { # close resets $. when we get to the end of each file that <> is processing close ARGV if eof; }