Forrest logo
back to the top tool

top:tldr:9652b

top: Show only the processes with the given PID(s), passed as a comma-separated list. (Normally you wouldn't know PIDs off hand. This example picks the PIDs from the process name).
$ top -p $(pgrep -d ',' ${process_name})
try on your machine

This command is a combination of two commands: pgrep and top.

The pgrep command is used to retrieve the process ID (PID) of a given process name. It searches for processes based on their names and returns the corresponding PIDs. In this specific command, ${process_name} is a placeholder that should be replaced with the name of the process you want to monitor.

The top command is a real-time process monitoring tool in Linux. It provides detailed information about running processes, resource usage, and system statistics. By default, it shows a dynamic view of the most resource-intensive tasks.

The command $(pgrep -d ',' ${process_name}) is enclosed within $(), which means that the output of the pgrep command will be substituted into the top command.

Here's how the whole command works:

  1. pgrep -d ',' ${process_name} searches for processes with the specified name and returns their PIDs, separated by commas. For example, if ${process_name} is "nginx", the command might output something like "1234,5678,9012".

  2. The output of the pgrep command is then substituted into the main top command using $(). So, the resulting command becomes: top -p 1234,5678,9012.

  3. Finally, when you execute the top command with the -p option followed by a comma-separated list of process IDs, it will display real-time information only for those specific processes. In this case, it will show stats only for the processes with the specified PIDs obtained from the pgrep command.

This command is useful when you want to monitor specific processes in the top output instead of seeing information for all running processes.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the top tool