Exclude grep From ps Results on Linux


The ps command in Linux is used to display information about the running processes on a system. It provides a snapshot of the current processes, including the process ID (PID), the user that owns the process, the percentage of CPU and memory usage, and the command that started the process. By default, ps only shows information about processes that are running in the same terminal session as the ps command. However, using various options and command line arguments, you can customize the output to show information about all processes running on the system, or even remotely.

Listing All Processes

To list all processes running on a Linux or Unix-like system using the ps command, you can use one of the following command options and arguments −

  • ps aux − This command shows all processes running on the system, including processes owned by other users. The a option shows processes for all users, the u option shows detailed information about the user running the process, and the x option shows processes that do not have a controlling terminal.

  • ps -e or ps -ef − This command shows all processes on the system, including the processes without a controlling terminal. The e option shows all processes and the f option displays the full format listing including the parent-child relationship of the processes.

  • ps -A − This command show all running processes, including those without a terminal controlling.

  • ps -eLf − It shows all processes in a tree format that show parent-child relationship as well as the thread of a process

Example

Here is an example of using the ps aux command to list all running processes on a Linux system −

$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  21644  4100 ?        Ss   Jan11   0:08 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Jan11   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Jan11   0:00  [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S    Jan11   0:00  [kworker/0:0]
root         5  0.0  0.0      0     0 ?        S    Jan11   0:00  [kworker/0:0H]
...

This command output shows the following information for each process −

  • USER − The user that owns the process

  • PID − The process ID

  • %CPU − The percentage of CPU usage

  • %MEM − The percentage of memory usage

  • VSZ − Virtual memory size

  • RSS − Resident set size

  • TTY − The terminal associated with the process

  • STAT − The process state

  • START − The time when the process started

  • TIME − The total CPU time consumed by the process

  • COMMAND − The command that started the process

Looking for a Specific Process

You can use the ps command in conjunction with other commands, such as grep or awk to search for a specific process. Here's an example of how you can use the ps command to search for a specific process and display only the relevant information −

$ ps aux | grep ssh
root      1027  0.0  0.1  47320  3304 ?        Ss   Jan11   0:00 /usr/sbin/sshd -D

This command uses the ps aux command to list all running processes, and pipes the output to grep ssh which filters the output to show only the lines that contain the string "ssh". In this example, the output shows that the ssh daemon process is running and the pid is 1027

Another way to find a specific process is

$ ps -ef | grep ssh
root     1027     1  0 Jan11 ?        00:00:00 /usr/sbin/sshd -D

This command uses the ps -ef command to list all running process, and pipes the output to grep ssh, that filters the output to show only the lines that contain the string "ssh". In this example, the output shows that the ssh daemon process is running, the pid is 1027, parent pid is 1 and other details as well.

You can also use the pgrep command to find the pid of the process you're looking for, by running

$ pgrep -fl ssh
1027 /usr/sbin/sshd -D

pgrep takes a -f option that match against the entire command line. and -l option to show the name of the command

Excluding grep

If you want to list all processes and filter out a process that contains a specific string, you can use the ps command with grep -v to exclude the string you want to filter out.

For example, to exclude all processes that contain the string "grep" from the output −

$ ps aux | grep -v grep

This command will display all the process except the ones that have the string grep in the command line.

Here is another way using the ps command alone −

$ ps aux --not-heading | awk '!/grep/ { print $0 }'

This command will display all process except the ones that have the string grep in the command line, Using the --not-heading option will exclude the headers, and awk with ! and /grep/ pattern matches the lines that do not contain the string 'grep' and prints them.

You can replace grep with any string that you want to exclude from the output.

Make a grep Expression That Excludes grep Itself

If you want to use the grep command to filter out processes that contain a specific string, but also exclude the grep command itself from the output, you can use the -v option to invert the match and the -w option to match only the whole word −

$ ps aux | grep -w -v grep

This command uses ps aux to list all running processes, and pipes the output to grep -w -v grep, which filters the output to exclude any lines that contain the exact string "grep" and also matches only the whole word.

Another way you can achieve this is by using the awk command

$ ps aux | awk '$11 !~ /grep$/ {print}'

This command uses ps aux to list all running processes, and pipes the output to awk command, which filters the output to exclude any lines that contain the exact string "grep" at the end of the command field. $11 is the field number of the command field, and !~ is a regular expression match negation.

Conclusion

In conclusion, the ps command is a useful tool for listing and monitoring processes running on a Linux or Unix-like system. You can use the various command options and arguments of the ps command to display information about all running processes, or filter the output to show only specific processes. Also, you can use other commands like grep, awk, pgrep to filter the output. You can also use grep with the -v option and -w option to exclude a specific process from the output, as well as awk with a regular expression to match the negation of the string. Keep in mind that the ps command's output format and options may vary across different Linux distributions and versions.

Updated on: 24-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements