Many UNIXes now have pgrep
which does exactly what you want
DESCRIPTION
pgrep looks through the currently running processes and lists the process IDs which
matches the selection criteria to stdout. All the criteria have to match.
As an example:
$ps -ef | grep sendmail
simonp 6004 27310 0 09:16 pts/5 00:00:00 grep sendmail
root 6800 1 0 Jul19 ? 00:00:03 sendmail: accepting connections
smmsp 6809 1 0 Jul19 ? 00:00:01 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue
$pgrep sendmail
6800
6809
The parameter passed to pgrep
is a regular expression - this is matched against either against the executable file name or the full process argument string dependent on parameters (-f
).
$pgrep '^sen.*il$'
6800
6809
$pgrep -f '^sendmail.*connections$'
6800
For more information
man pgrep