Grep trick for locating processes

Written by

in

Yesterday Dominis mentioned shell-foo, and one cool thing that one of my ex-colleagues showed me a few years ago came to mind. (Hi Pali!)

So let’s pretend you have to locate the pid of a mysql process. You run the good old ps command and grep for mysql:

[[email protected] ~]$ ps -ef | grep mysql
root      1751     1  0 Jul01 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/sql-master1.bfc.kinja.com.pid
mysql     2424  1751 88 Jul01 ?        21:59:18 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/var/lib/mysql/sql-master1.bfc.kinja.com.pid --socket=/var/lib/mysql/mysql.sock
banyek    4630  4360  0 11:31 pts/0    00:00:00 grep mysql
[[email protected] ~]$

There is one small problem: grep will find itself too, so if you want to count the number of processes it will show one more, or if you only want to find one process by its exact pid, you have to deal with the extra line in the input, etc.
There is an old-fashioned way to filter the results with another grep like this:

[[email protected] ~]$ ps -ef | grep mysql | grep -v grep
root      1751     1  0 Jul01 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/sql-master1.bfc.kinja.com.pid
mysql     2424  1751 90 Jul01 ?        22:25:20 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/var/lib/mysql/sql-master1.bfc.kinja.com.pid --socket=/var/lib/mysql/mysql.sock
[[email protected] ~]$

It is much better, but it has two problems:

The first is that the shell will fork a new grep process (ok, this is not a real problem, but …) and the second is that you shoot yourself in your legs if you try to find a grep process on the system.

At this point you can use the grep regex match feature, and use it in the process filter like this:

[[email protected] ~]$ ps -ef | grep [m]ysql
root      1751     1  0 Jul01 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/sql-master1.bfc.kinja.com.pid
mysql     2424  1751 90 Jul01 ?        22:34:07 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/var/lib/mysql/sql-master1.bfc.kinja.com.pid --socket=/var/lib/mysql/mysql.sock
[[email protected] ~]$

What happened? Grep used [m] as a regex character which only matches the letter ‘m’ – so the output is the same as before, but the ‘[m]ysql’ didn’t match ‘mysql’, so it was clearly filtered out.

Cool, huh?