Linux’s top command-line utility displays information about currently active processes and system resources. Seeing what a Linux system is doing in real time makes it easier to find processes that use a lot of resources or to simply monitor loads. For example, we can use top to monitor the uptime, memory usage, and CPU load of our system or a specific process.
In this tutorial, we’ll see how to use the top command in Linux to filter processes based on their names. First, we can make use of the top built-in facility for our needs. Second, we can write a custom script. It will automatically refresh itself and update the status of the target process.
2. Filtering Processes Using the top Command
To filter processes by name when the top command is running, we can use the O key followed by entering a filter expression.
For example, we’ll single out the apache2 process via a filter expression in the top command prompt.
Let’s first open a terminal window and enter the top command. This displays a list of processes currently running on our system:
$ top PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 16 root 20 0 0 0 0 R 81.7 0.0 15:53.89 kworker+ 1352 root 20 0 7224 1316 704 S 1.0 0.5 0:09.76 Xorg 1373 root -51 0 0 0 0 S 0.7 0.0 0:03.50 irq/132+
Now, let’s press the O key, which brings up a prompt asking us to enter the field and value to filter.
In our case, the field is COMMAND, while the value should be apache2. Hence, we join these strings with an equals sign, so our filter to single out apache2 becomes COMMAND=apache2:
... add filter #1 (ignoring case) as: [!]FLD?VAL COMMAND=apache2
Finally, the top command should now only display the statistics of the apache2 process:
top - 03:46:28 up 9 min, 1 user, load average: 0.00, 0.08, 0.08 Tasks: 110 total, 1 running, 109 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 976.6 total, 189.8 free, 146.9 used, 639.9 buff/cache MiB Swap: 1953.0 total, 1953.0 free, 0.0 used. 678.2 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1825 root 20 0 6524 4412 3220 S 0.0 0.4 0:00.00 apache2 1691 www-data 20 0 1211412 4548 2988 S 0.0 0.5 0:00.00 apache2 1692 www-data 20 0 1211412 4552 2992 S 0.0 0.5 0:00.00 apache2
Notably, there can be several processes with the same name, but different PIDs.
3. Filtering Processes Using a Script
To filter a process, we can also use the pgrep command along with the top command. The -f flag of pgrep gets the matching PIDs of the target processes from their command line:
$ top -c -p $(pgrep -d',' -f matching_string_in_cmdline_output)
However, when we restart a process, its PID changes. Consequently, the top command needs to restart to retrieve the full list of target process PIDs.
To resolve this problem, we can use a script to filter the results of the top command by process name. For example, let’s consider the script (my_script.sh) below:
#!/bin/bash __process_name=$1 while (true) do clear __arg=$(pgrep -d',' -f $__process_name) if [ -z "$__arg" ]; then top | head -n 5 else top -c -n 1 -p $__arg fi sleep 5 done
Let’s break down this script and see the meaning of the various commands used here:
- __process_name: name of the variable which sets the name of the process from the first command line argument ($1)
- while loop: runs the process list iteration indefinitely until the script is interrupted
- clear: clears the terminal screen for each iteration, similar to watch
- pgrep: the -d option specifies a delimiter to use when printing the process IDs, while -f is used to match the full command line of a process instead of just the process name
- __arg: stores the output (the list of processes) from the pgrep command
- if block: the -z option checks if a given string is empty, so we know whether any PIDs are returned and only use them as a filter when they are returned
Basically, the script keeps the output updated with data about the process with the given name. Here, the refresh interval is set to 5 seconds using the sleep command.
Let’s take the example of the apache2 process and run our script as:
$ ./my_script.sh apache2
Consequently, the top command continuously displays all the apache2 processes:
Tasks: 3 total, 0 running, 3 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 976.6 total, 169.3 free, 155.1 used, 652.3 buff/cache MiB Swap: 1953.0 total, 1953.0 free, 0.0 used. 669.1 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3364 root 20 0 6524 4484 3296 S 0.0 0.4 0:00.01 /usr/sbin/apac+ 3365 www-data 20 0 1211412 4544 2984 S 0.0 0.5 0:00.00 /usr/sbin/apac+ 3366 www-data 20 0 1211412 4544 2984 S 0.0 0.5 0:00.00 /usr/sbin/apac+
Also, when a process restarts, we don’t have to press the Q key or CTRL+C and run top again and again. The above script takes care of this.
In this article, we’ve seen how to filter by process name the output of the top command.
First, we used the top command prompt to interactively enter the filter expression for filtering processes based on their names. Second, we created a custom script that displays a specific process in top’s output and refreshes the output at intervals.