I gave up on pgrep
long ago. Always seemed like it should be more useful that what it ends up being.
This is what I use. A bit long winded, and always thought there must be a better way, so maybe someone can enlighten us both.
egrep
allows for filtering with extended regular expressions which becomes important when you only want "one answer". grep -E
is supposed to do the same thing but I got in the habit of using egrep
because I wanted a solution which worked across multiple flavors of *nix.
The -e
flag tells ps
to show all processes (runs with permission of executing user. ie: not all procs show unless you are root). The-f
flag tells ps
to issue a "full-format" listing which you might need to | less
when viewing it since longer CLI args might scroll off the right of the terminal and might not wrap. Hope it helps.
Find processes matching agetty
:
# ps -ef | egrep agetty
root 1386 1 0 Sep19 ttyS0 00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root 1388 1 0 Sep19 tty1 00:00:00 /sbin/agetty --noclear tty1 linux
root 28332 4228 0 11:23 pts/2 00:00:00 grep -E agetty
Find process matching agetty
that also have baud
in them:
# ps -ef | egrep 'agetty.*baud'
root 1386 1 0 Sep19 ttyS0 00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root 28347 4228 0 11:23 pts/2 00:00:00 grep -E agetty.*baud
Don't return my grep
process. Not sure how it works but saves you from having to | grep -v 'grep'
on the results. Maybe someone could explain this:
# ps -ef | egrep 'agetty.*b[a]ud'
root 1386 1 0 Sep19 ttyS0 00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
egrep with the OR condition
# ps -ef | egrep '(ag[e]tty|ac[p]id)'
root 944 1 0 Sep19 ? 00:00:00 /usr/sbin/acpid
root 1386 1 0 Sep19 ttyS0 00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root 1388 1 0 Sep19 tty1 00:00:00 /sbin/agetty --noclear tty1 linux
root 28395 4228 0 11:28 pts/2 00:00:00 grep -E (agetty|acpid)