22

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?

32
ps -ef | egrep "GMC|PID"

Replace the "GMC" and ps switches as needed.

Example output:

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
  • 7
    Would be good to add some info on 'why' this works. – Elijah Lynn Jan 29 '16 at 20:21
  • 2
    No, that's an exercise for the user. – Hyppy Jan 29 '16 at 20:23
  • @ElijahLynn It matches text in the header - in this case, PID. But you could swap it out for UID, PTIME, or anything else in the header... – Ben Creasy Feb 13 '17 at 21:51
  • 4
    So ps -e selects all processes, and ps -f is full-format listing which shows the column headers. Then we pipe the column headers and output to egrep, which is extended grep and allows the pipe | to have a special meaning, which is OR (this OR that). So you end up matching the PID in the column headers plus the output lines that matter. – Elijah Lynn Feb 16 '17 at 20:43
  • I had to use simple quotation marks in the egrep/grep -E command in Ubuntu 16.04, e.g.: ps -ef | grep -E 'GMC|PID' – Vlax Apr 3 '17 at 8:49
13

Thanks to geekosaur, I would like to use this command for your demands, rather than a separated command:

ps -ef | head -1; ps -ef | grep "your-pattern-goes-here"

The tricky is to make use of the ";" supported by the shell to chain the command.

  • this is cleaner and more robust than the accepted answer. It does not assert that the header has PID in it and does not add complexity to the grep string. – 7yl4r Aug 2 '17 at 15:03
  • 1
    oh wait... you've got the ps -ef repeated. even better is ps -ef | { head -1 ; grep "your-pattern" ; } – 7yl4r Aug 2 '17 at 18:52
  • @7yl4r never used that shell ability. I tried your improved command, works perfectly! Got learned, :) – Vic Lau Aug 3 '17 at 2:38
  • 2
    Found explanation about this technic, called 'Grouping Commands', for others' curiosity, see: gnu.org/software/bash/manual/html_node/Command-Grouping.html – Vic Lau Aug 3 '17 at 2:53
6

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 using ps's own selection methods or something like pgrep instead of grep, which isn't really designed to pass headers through.

  • What's the 83 ? – Webnet May 11 '11 at 20:39
  • Current process priority, which is based on its past CPU and I/O usage and the user- or system-assigned nice value. Smaller numbers are higher priority. In this case, grep is priority 0 because it was blocked on disk reads and yielded to write its output, and PNetTNetServer.bin is a large number because it consistently uses up its timeslice without blocking. (Scheduling is complex and the details will depend on the exact scheduler being used.) – geekosaur May 11 '11 at 20:46
4

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:

ps auxwww | sed -n '1p; /PROCESS_NAME_TO_SEARCH/p;'

Add /sed -n/d; to filter sed itself out:

ps auxwww | sed -n '1p; /sed -n/d; /PROCESS_NAME_TO_SEARCH/p;'
  • /sed -n/d is not right. Some existing command might have sed -n, which you wanted to print. Trick is to use sed -n '1p; /[P]ROCESS_NAME_TO_SEARCH/p'. ;-) Note [] around any character in the search string. – anishsane Jul 12 '18 at 6:08
4

easier alternative: ps -ef | { head -1; grep GMC; }

replace the number with the number of lines your header is displayed on.

  • 1
    I like this approach but the command needs another semicolon at the end. ps -ef | { head -1; grep GMC; }. Also I like it in a function like the following: function pgrep() { ps -ef | { head -1; grep $@; } } – Brett Jan 31 '17 at 17:31
1

you could get the pid with pgrep

pgrep PNetTNetServer

and then use ps with the pid

ps u 12345

or even combine the two into one command

ps u `pgrep PNetTNetServer`

This would show just the line you want, and include the header.

0

I wrote a small Perl program that will print

  • the first line and all lines matching, if there are any matches, or
  • nothing, if there are no matches.

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).



#!/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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.