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

Until today, in spite of the growing comfort with many and varied Linux commands, I have not tried to understand some of the components of https://stackoverflow.com/a/312960/ ... filter the top command for specific processes (by process name):

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

This top command contains various syntax components. I could search for each component in turn (and some of them are obvious, for example, a pipe command, although I'm still not 100% comfortable with the difference between the | channel and the redirection > ).

However, since filtering the top command by process name is often useful, and since the command on its face is clearly not trivial in terms of syntax, it would be useful for me (and possibly others) to have a link to the actual (short) explanation of all parts of this teams in one place.

Therefore, my question is as follows.

What is a brief explanation of all the components of the above top command (which filters the top output by process name)? I would be grateful if I had not been left without syntax, even if just a short mention. Thank!

0
linux bash grep sed
Jul 31 '14 at 18:12
source share
4 answers

I will try to break this by answering your question in pieces.

The first part we'll look at is sed 's/,$//' . I will not explain briefly the sed command, but please see the GNU sed manual for more information. 's/,$//' tells sed to look for any commas at the end of the line and does not replace them with anything. This removes all the commas from the end of each line on standard sed input.

The next snippet we'll look at is tr "\\n" "," . Again, tr can do many things, but its goal is to convert characters. See tr man page for more details. This usage should include all UNIX newline characters ( \n ) with a comma. \ must be escaped with an extra \ , because the command line will treat \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 identifier (pid) of any running process that matches the selection criteria to the standard one. Each pid will be printed on a separate line. Here we are looking for the pid of the process-name command.

Now for the pipe ( | ). This creates a new process and sends everything that is printed to the standard standard in the new process.

Let's look at the teams to understand how the pipe works. pgrep process-name | tr "\\n" "," pgrep process-name | tr "\\n" "," will print all pid process-name matches in a standard way, which is passed to the standard input of the tr process. tr "\\n" "," | sed 's/,$//' tr "\\n" "," | sed 's/,$//' converts all the lines of a newline from its standard to commas and prints the result to its standard output, which is passed to the new process sed 's/,$//' .

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

Two more parts: top -p and backward labels (``). Backticks creates a new process that runs commands inside and replaces its standard before computing the entire team. For example:

 echo `echo "Hello, World!"` 

displays Hello, World! .

Finally, top -p accepts a list of messages and reports of various information, such as runtime, memory usage, and nice value. A more detailed explanation can be found on the page.

+2
Jul 31 '14 at 18:59
source share

Piping takes the output of the command on the left and uses it as the input of the command on the right. Thus, the output of pgrep goes into tr , and then sed goes. Return lines mean executing commands and returning something. In this case, top -p looks for the process ID.

Brief explanation:

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

Bit longer:

  • pgrep is basically a grep process name in ps aux
  • tr replaces all \\n with a ,
  • sed deletes , ( $ means end of line, which is used as fault tolerant)
  • This output is passed to top -p , which provides a dynamic representation of the current system in real time. It can display system summary information, and the list of tasks currently managed by the Linux kernel

Very long:

  • man pgrep
  • man tr
  • man sed
  • man top
+2
Jul 31 '14 at 18:22
source share
 pgrep process-name | tr "\\n" "," | sed 's/,$//' 

This part gives a list of processes separated by commas, which is assigned to the top command, which displays the use of resources.

It can be rewritten as:

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

1) pgrep process-name - get a list of process-name process identifiers (if executed).
2) tr "\\n" "," - the output of the pgrep command is on separate lines. Thus, this part deletes new lines (if any) and separates them with commas.
3) sed 's/,$//' - this part removes the trailing comma. $ means "end of line", and ,$ denotes a trailing comma. s/,$// says that instead of a comma, "end of line" (ie the last) is nothing, i.e. remove the trailing comma.

eg.

If pgrep process-name gives an output, for example:

 4560 5000 

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

+1
Jul 31 '14 at 18:28
source share

One of the syntax top -p is -pN1,N2,N3 ... , in which N1, N2, and N3 are process identifiers. pgrep gets the process identifier that matches the process-name , and tr "\\n" "," | sed 's/,$//' tr "\\n" "," | sed 's/,$//' tries to convert the output to comma separated values. This csv result is passed as the top -p argument because it is a submenu of the command.

For easier understanding, go to the steps with the variables:

Get process IDs.

 PIDS=$(pgrep process-name) 

Convert newline characters to comma.

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

Remove the extra comma at the end. This may not be necessary in our steps, thanks to the substitution of commands, chopping an additional line of a new line at the end, but this is necessary with the help of a pipe.

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

Run the top.

 top -p "$CSV" 

This completes the command, although it can be more efficient and convenient if we used the -d pgrep option, which sets the output delimiter:

 top -p "$(pgrep process-name -d ,)" 
+1
Jul 31 '14 at 18:30
source share



All Articles