How can I grep the PS output with the headers in place?
These two process make up an app running on my server....
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
does 6-22:01:17
mean that it's been running for 6 days? I'm tring to determine the length of how long the process has been running...
Is the 2nd column the process id? So if I do kill 32017
it'll kill the 2nd process?
Replace the "GMC" and
ps
switches as needed.Example output:
ps -e
selects all processes, andps -f
is full-format listing which shows the column headers.Thanks to geekosaur, I would like to use this command for your demands, rather than a separated command:
The tricky is to make use of the ";" supported by the shell to chain the command.
Second column is the process id; 4th is when the process was created (this is usually the time your program started, but not always; consider
execve()
and friends); 6th is the amount of CPU time consumed. So it's been running for 8 days and used almost 7 days of CPU time, which I would consider worrisome.Getting the header in the same invocation is tricky at best; I'd just do a separate
ps | head -1
. You might consider usingps
's own selection methods or something likepgrep
instead ofgrep
, which isn't really designed to pass headers through.easier alternative:
ps -ef | { head -1; grep GMC; }
replace the number with the number of lines your header is displayed on.
The egrep solution is simple and useful, but of course you depend on the header always containing 'PID' (a more than reasonable assumption, though) and the same string not ocurring elsewhere. I'm guessing this is enough for your needs, but in case someone wants an alternative there's sed.
Sed lets you just say "print the first line, then any line containing the pattern". For example:
Add
/sed -n/d;
to filter sed itself out:you could get the pid with pgrep
and then use ps with the pid
or even combine the two into one command
This would show just the line you want, and include the header.
I wrote a small Perl program that will print
I most often use it like
ps | 1andre GMC
, but it can also take file arguments (each file provides its own header line for matches made on lines from that file).
This works for me at Solaris and several Linux flavors. it's pretty similar to the first:
It's a 2 command part:
And the best thing (other example how I use it with multiple patterns) is:
This search's a script that have tmp_usage and du command... And you can add more details every pipe. Output sample:
I hope you like it!
Juan