GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script for top.
Operating System - HP-UX
1848490
Members
6820
Online
104029
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2002 09:45 AM
07-15-2002 09:45 AM
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.
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.
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2002 10:04 AM
07-15-2002 10:04 AM
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
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2002 10:19 AM
07-15-2002 10:19 AM
Re: Script for top.
Hi Tom:
Thanks. Can you explain the awk statement..like what it does??
Thanks
Joe.
Thanks. Can you explain the awk statement..like what it does??
Thanks
Joe.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2002 10:35 AM
07-15-2002 10:35 AM
Solution
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
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!
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2026 Hewlett Packard Enterprise Development LP