{"id":13,"date":"2013-07-02T00:23:54","date_gmt":"2013-07-01T23:23:54","guid":{"rendered":"http:\/\/188.166.119.205\/?p=13"},"modified":"2026-11-10T05:52:05","modified_gmt":"2026-11-10T05:52:05","slug":"grep-trick-for-locating-processes","status":"publish","type":"post","link":"https:\/\/banyek.net\/?p=13&lang=en","title":{"rendered":"Grep trick for locating processes"},"content":{"rendered":"<p>Yesterday Dominis mentioned shell-foo and one cool thing came to my mind what one of my ex-colleagues showed me a few years ago. (Hi Pali!)<\/p>\n<p>So let\u2019s pretend you have to locate pid of mysql process. You run the good old ps command, and grep for mysql:<\/p>\n<p><!--more--><\/p>\n<pre><code>[banyek@sql-master1.bfc ~]$ ps -ef | grep mysql\nroot      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\nmysql     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\nbanyek    4630  4360  0 11:31 pts\/0    00:00:00 grep mysql\n[banyek@sql-master1.bfc ~]$\n<\/code><\/pre>\n<p>There is one small problem: the 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 with exact pid you have to deal with the plus line in input etc.<br \/>\nThere is an old-fashioned way, to filter the results with an another grep like this:<\/p>\n<pre><code>[banyek@sql-master1.bfc ~]$ ps -ef | grep mysql | grep -v grep\nroot      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\nmysql     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\n[banyek@sql-master1.bfc ~]$\n<\/code><\/pre>\n<p>It is much better, but have two problems:<\/p>\n<p>The first, the shell will fork a new grep process (ok, this is not a real problem, but \u2026) and the second is you shoot you in your legs if you try to find a grep process on the system.<\/p>\n<p>At this point you can use the grep regex match feature, and use it in the process filter like this:<\/p>\n<pre><code>[banyek@sql-master1.bfc ~]$ ps -ef | grep [m]ysql\nroot      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\nmysql     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\n[banyek@sql-master1.bfc ~]$\n<\/code><\/pre>\n<p>What happened? The grep used [m] as a regex character which only matches letter \u2018m\u2019 \u2013 so the output is the same as before, <em>but the \u2018[m]ysql\u2019 didn\u2019t match \u2018mysql\u2019, so it was clearly filtered out.<\/em><\/p>\n<p><em>Cool, huh?<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday Dominis mentioned shell-foo and one cool thing came to my mind what one of my ex-colleagues showed me a few years ago. (Hi Pali!) So let\u2019s pretend you have to locate pid of mysql process. You run the good old ps command, and grep for mysql:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[126,236],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-tech-en","tag-oldblog-en","tag-shell-fo-en"],"_links":{"self":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":1,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":448,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions\/448"}],"wp:attachment":[{"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}