Showing posts with label grep. Show all posts
Showing posts with label grep. Show all posts

2013/05/09

Grep running process

If you need script to see if specified process is running, its easy to grep it from ps aux.

 m@box:~$ ps aux | grep mysqld  
 mysql   1090 0.0 3.1 322660 48892 ?    Ssl 19:53  0:00 /usr/sbin/mysqld  
 m       1903 0.0 0.0  4384  808 pts/0  S+  20:29  0:00 grep --color=auto mysqld  

Problem here is that you can see your grep process which has your process as parameter. Script that relays on exit code ends up always being successful since grep process is on the list. You can easily fix this problem by adding additional pipe trough grep with -v grep parameter.

-v option is also --invert-match

 ps aux | grep mysqld | grep -v grep 1>/dev/null&& echo Process is runing || echo Process is not running  

Its a lot easier to do this by using pgrep.

pgrep scans running processes and returns process pid into your stdout.

 m@box:~$ pgrep mysqld 1>/dev/null&& echo Process is runing || echo Process is not running  
 Process is runing