Linux: Check if a process is running

You can list all the running processes on a Linux box with ps aux, but often you're looking for a specific process. This is pretty easily accomplished with grep:

ps aux | grep /usr/sbin/sshd

The problem with this is that you often pick up you own grep in the output:

$ ps aux | grep /usr/sbin/sshd
root         883  0.0  0.0  76640  7428 ?        Ss   Oct18   0:00 /usr/sbin/sshd -D -oCiphers...
bakers     11691  0.0  0.0  12148  1104 pts/0    S+   08:09   0:00 grep --color=auto /usr/sbin/sshd

The quick and dirty solution is to do some trickery with a regular expression and grep:

$ ps aux | grep -P '/usr/sbin/[s]shd'
root         883  0.0  0.0  76640  7428 ?        Ss   Oct18   0:00 /usr/sbin/sshd -D -oCiphers...

The square brackets tell grep to match a character class with only one character in it. This prevents grep from picking up itself, but still matches what you want.



Note: Replies will be formatted with PHP Markdown Extra syntax.

Name: Email (Not Required):
 
Logged IP: 3.227.252.87
To prevent spam please submit by clicking the kitten: