What is a brief explanation of the syntax of the following "top" command: top -p `pgrep process-name | tr "\\n" "," | sed 's/,$//'`
Asked Answered
G

4

0

Prior to today, despite growing comfort with many and varied Linux commands, I have not attempted to understand some of the components of a useful command from another StackOverflow posting to filter the top command to certain processes (by process name):

top -p `pgrep process-name | tr "\\n" "," | sed 's/,$//'`

This top command contains various syntactic components. I could look up each component in turn (and some are obvious, such as the pipe command - though I'm still not 100% comfortable with the distinction between the pipe | and redirection >).

However, because filtering the top command by process name is often useful, and because the command is on its face clearly not trivial from a syntax perspective, it would be useful for me (and perhaps others) to have a reference to an actual (brief) explanation for all of the pieces of this command, in one place.

Therefore, my question is the following.

What is a brief explanation of all of the components of the above top command (that filters the output of top by process name)? I would appreciate leaving no piece of syntax out, if even for just a brief mention. Thanks!

Garish answered 31/7, 2014 at 18:12 Comment(0)
T
2

I'll try to break it down by answering your question in pieces.

The first piece we'll look at is sed 's/,$//'. I'm not going to be able to briefly explain the sed command but please look at the GNU sed manual for more information. The 's/,$//' is telling sed to look for any commas at the end of a line and substitute them with nothing. This is removing all the commas from the end of each line on sed's standard input.

The next piece we'll look at at is tr "\\n" ",". Again, tr can do many things but its purpose is to transform characters. See tr man page for a more detailed explanation. This usage is to turn all UNIX newline characters (\n) into commas. The \ has to be escaped with an additional \ because the command line would treat the \n as a new line and break the command.

Next is pgrep process-name. For completeness here is the pgrep man page. pgrep simply prints the process id (pid) of any running process that matches the selection criteria to standard out. Each pid will be printed on a line by itself. Here we are looking for the pid of the command process-name.

Now for pipe (|). This creates a new process and sends anything printed on standard out to the standard in of the new process.

Lets look at the commands to understand how pipe works better. pgrep process-name | tr "\\n" "," will print all pid's matching process-name to standard out which is passed to the standard input of the process running tr. tr "\\n" "," | sed 's/,$//' will transform all newlines from its standard in to commas and print the result to its standard out which is passed to the new process running sed 's/,$//'.

So the entire command pgrep process-name | tr "\\n" "," | sed 's/,$//' will print a space separated line of pid's for any running process that has the name process-name.

Two more pieces to look at: top -p and the backticks (``). Backticks creates a new process that runs the commands inside and substitutes its standard out before the entire command is evaluated. For example:

echo `echo "Hello, World!"`

prints Hello, World! to the screen.

Lastly, top -p takes a list of pids and reports various information such as run time, memory usage and nice value. For a more detailed explanation see the top man page.

Tempting answered 31/7, 2014 at 18:59 Comment(0)
A
2

Piping takes the output of the command on the left and uses it as a the input of the command on the right. So the output of pgrep goes to tr and then goes the sed. The backticks mean execute the commands and return something. In this case top -p is looking for a process ID.

Brief Explanation:

Essentially it looks for the process of process-name does a little formatting via tr and sed and presents a readable number to top -p which gives memory, CPU usage, etc.

Slightly Longer:

  • pgrep basically grep a process-name in ps aux
  • tr substitutes all \\n for a ,
  • sed deletes , (the $ means end of string which is used as failsafe)
  • This output is passed into top -P which provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel

Very Much Longer:

  • man pgrep
  • man tr
  • man sed
  • man top
Amarillo answered 31/7, 2014 at 18:22 Comment(0)
T
2

I'll try to break it down by answering your question in pieces.

The first piece we'll look at is sed 's/,$//'. I'm not going to be able to briefly explain the sed command but please look at the GNU sed manual for more information. The 's/,$//' is telling sed to look for any commas at the end of a line and substitute them with nothing. This is removing all the commas from the end of each line on sed's standard input.

The next piece we'll look at at is tr "\\n" ",". Again, tr can do many things but its purpose is to transform characters. See tr man page for a more detailed explanation. This usage is to turn all UNIX newline characters (\n) into commas. The \ has to be escaped with an additional \ because the command line would treat the \n as a new line and break the command.

Next is pgrep process-name. For completeness here is the pgrep man page. pgrep simply prints the process id (pid) of any running process that matches the selection criteria to standard out. Each pid will be printed on a line by itself. Here we are looking for the pid of the command process-name.

Now for pipe (|). This creates a new process and sends anything printed on standard out to the standard in of the new process.

Lets look at the commands to understand how pipe works better. pgrep process-name | tr "\\n" "," will print all pid's matching process-name to standard out which is passed to the standard input of the process running tr. tr "\\n" "," | sed 's/,$//' will transform all newlines from its standard in to commas and print the result to its standard out which is passed to the new process running sed 's/,$//'.

So the entire command pgrep process-name | tr "\\n" "," | sed 's/,$//' will print a space separated line of pid's for any running process that has the name process-name.

Two more pieces to look at: top -p and the backticks (``). Backticks creates a new process that runs the commands inside and substitutes its standard out before the entire command is evaluated. For example:

echo `echo "Hello, World!"`

prints Hello, World! to the screen.

Lastly, top -p takes a list of pids and reports various information such as run time, memory usage and nice value. For a more detailed explanation see the top man page.

Tempting answered 31/7, 2014 at 18:59 Comment(0)
G
1
pgrep process-name | tr "\\n" "," | sed 's/,$//'

This part gives a comma separated list of process which is given to top command which displays the resources usage.

It can re-written as:

process_list=$(pgrep process-name | tr "\\n" "," | sed 's/,$//')
top -p ${process_list}

1) pgrep process-name - gets the list of process IDs of process-name (if running).
2) tr "\\n" "," - The output of pgrep command is in separate lines. So this part removes the newline(s) (if any) and separates them with commas.
3) sed 's/,$//' - This part removes the trailing comma. $ stands for "end-of-line" and ,$ stands for trailing comma. s/,$// says substitute the "end-of-line" (i.e. last) comma with nothing i.e. remove trailing comma.

E.g.

If pgrep process-name gives an output like:

4560
5000

Then it first becomes: 4560,5000, ( by tr).
Then becomes 4560,5000 (by sed)
and then finally passed to top as: top -p 4560,5000 which would display only these two processes.

Glendoraglendower answered 31/7, 2014 at 18:28 Comment(0)
C
1

One of top -p's syntax is -pN1,N2,N3 ... in which N1, N2 and N3 are process IDs. pgrep gets the ID's of processes that matches process-name while tr "\\n" "," | sed 's/,$//' tries to convert the output to comma separated values. This resulting csv is passed as an argument to top -p due to the fact that it's under command substitution.

To make it easier to understand let's get to the steps with variables:

Get process IDs.

PIDS=$(pgrep process-name)

Convert newlines to commas.

CSV=$(echo -n "$PIDS" | tr "\\n" ",")

Remove extra comma at the end. This may not be necessary in our steps thanks to command substitution chopping out the extra newline at the end but it's necessary with the pipe.

CSV=$(echo -n "$CSV" | sed 's/,$//')

Run top.

top -p "$CSV"

That completes the command although it could have been more efficient and easier if we used -d option of pgrep which specifies the output delimiter:

top -p "$(pgrep process-name -d ,)"
Catcall answered 31/7, 2014 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.