I use ps command to find out all running process on my Linux and Unix system. The ps command shows information about a selection of the active processes on shell. You may also pipe out ps command output through grep command to pick up desired output.
Let us run a combination of ps command and grep command to find out all Perl processes:
$ ps aux | grep perl
vivek 4611 0.0 0.7 10044 6068 ? Ss 02:40 0:00 /usr/bin/perl apps/monitor/gwl.pl root 4853 0.0 0.7 10044 6068 ? Ss 02:40 0:00 /usr/bin/perl /usr/share/webmin/miniserv.pl /etc/webmin/miniserv.conf vivek 5166 0.0 0.0 2884 748 pts/0 R+ 03:06 0:00 grep perl
In above example, I am getting the grep process itself. To ignore grep process from the output, type any one of the following command at the CLI:
$ ps aux | grep '[p]erl'
$ ps aux | grep perl | grep -v grep
vivek 4611 0.0 0.7 10044 6068 ? Ss 02:40 0:00 /usr/local/bin/perl5 apps/monitor/gwl.pl root 4853 0.0 0.7 10044 6068 ? Ss 02:40 0:00 /usr/local/bin/perl5 /usr/share/webmin/miniserv.pl /etc/webmin/miniserv.conf
The above output indicate that I prevented ‘grep’ from showing up in ps results.
Understanding above commands
You don’t want display grep command as the process in ps output, i.e., you want to prevent ‘grep’ from showing up in ps results.
- In first command I used regex. It says find the character ‘p’ followed by ‘erl’ i.e. the expression ‘[p]erl’ matches only ‘perl’ not ‘[p]erl’, which is how the grep command itself is now shown in the process list.
- The second command uses the -v option to invert the sense of matching, to select non-matching lines.
Say hello to pgrep
You can look up process based upon name to get PID. This is only useful when looking for process names and PIDs The syntax is:
pgrep -a process
pgrep -l process
pgrep -u user -a process
To find the process ID of the sshd daemon:
$ pgrep -u root sshd
To list PID and full command line pass the -a to the pgrep command:
$ pgrep -a sshd
1101 /usr/sbin/sshd -D 5494 /usr/sbin/sshd -D 6041 /usr/sbin/sshd
To just list PID and process name pass the -l to the pgrep command:
$ pgrep -l firefox