1848480 Members
6227 Online
104029 Solutions
New Discussion

Script for top.

 
SOLVED
Go to solution
joe_91
Super Advisor

Script for top.

Hi All:

I was able to pick up a good script from the forums which i have attached. This picks out the top process(first line) of the top command.

In our case here we sometimes have two application processess eating up CPU and we want to gather some info before killing it.I understand that this script would work for the second process also after the 1st one is killed. But how do i modify it so that it works for 2 top processes?

Thanks
Joe.
3 REPLIES 3
Tom Maloy
Respected Contributor

Re: Script for top.

First, ask for two processes from top: change the -n1 option to -n2. You may want to use -h as well, to hide multiple CPU output.

Second, change that first awk line from:

# Print PID, CPU usage, username, and process name of single top process
awk '{if(($1~/CPU/)&&($2~/TTY/)){flg=1;next};if(flg==1){printf("%6s %3.0f %9s %s\n",$3,$11,$4,$NF);flg=0}}' /tmp/xxx >/tmp/yyy

to:

# Print PID, CPU usage, username, and process name of two top processes
awk '{if(($1~/CPU/)&&($2~/TTY/)){getline; printf("%6s %3.0f %9s %s\n",$3,$11,$4,$NF);getline; printf("%6s %3.0f %9s %s\n",$3,$11,$4,$NF)}' /tmp/xxx >/tmp/yyy

The "if" matches the header line of the data section. "getline" gets the data for the first process, printed with "printf". Repeat getline and printf for the second process.

Tom
Carpe diem!
joe_91
Super Advisor

Re: Script for top.

Hi Tom:

Thanks. Can you explain the awk statement..like what it does??

Thanks
Joe.
Tom Maloy
Respected Contributor
Solution

Re: Script for top.

Let's look at the whole thing, and break it down into pieces. The whole thing:

awk '{if(($1~/CPU/)&&($2~/TTY/)){getline; printf("%6s %3.0f %9s %s\n",$3,$11,$4,$NF);getline; printf("%6s %3.0f %9s %s\n",$3,$11,$4,$NF)}}' /tmp/xxx >/tmp/yyy

Note that I added one other } before the last ' to close the if statement.

The general form is:
awk 'pattern{commands}' inputfile > outputfile

So awk is getting its input from /tmp/xxx, and putting its output into /tmp/yyy.

In this awk script, we do not have any of the (optional) patterns. (If patterns were present, then awk would execute to commands if and only if the pattern was matched. I'm sure that we could change this script to use patterns if needed.)

With no patterns, we will execute the commands for every line of input.

The only command is an if statement. If the first parameter of the input line ($1) is "CPU" and the second parameter ($2) is "TTY", then the commands inside the second set of {} will be executed.

getline throws out the current line of data, and gets the next line from the input file. That will be the line containing data for the first process. printf just formats the output. In this case, we are printing out four values: the third ($3), eleventh ($11), fourth (4$), and total number of fields on the line ($NF).

To get the data for the second process, we just repeat the getline (which puts new values in $1, $2, ..., $11, ..., and $NF) and printf.

HTH.

Tom


Carpe diem!