Operating System - HP-UX
1827294 Members
1268 Online
109960 Solutions
New Discussion

Outputting top information to a text file

 
Andrew Kaplan
Super Advisor

Outputting top information to a text file

Hi there --

I would like to use top to take a snapshot of a system's resource usage, and output the results to a text file. What arguments and/or options would I use with top to accomplish this? Thanks.
A Journey In The Quest Of Knowledge
8 REPLIES 8
Robert-Jan Goossens
Honored Contributor

Re: Outputting top information to a text file

Hi Andrew,

# top -d 1 -f output_file

But I think you best look at the sar command for analyzing system performance.

Regards,
Robert-Jan

Dave Hutton
Honored Contributor

Re: Outputting top information to a text file

top -f /tmp/filename.txt

That will give you snapshot of a moment.
Andrew Kaplan
Super Advisor

Re: Outputting top information to a text file

Hi there --

Thanks for your reply. I completed writing the script, and here is the text of it:

# This script will run the top utility, and save the results to a text file.
# The text file will then be e-mailed to the system administrators.

# Change to the directory where the output file will be generated by this script.
cd /tmp

# Run the top utility as sudo user, and save the results to a text file.
sudo top -f topoutput.txt

# E-mail the outputfile that was generated, to the system administrators.
mail < topoutput.txt
mail < topoutput.txt
mail < topoutput.txt
mail < topoutput.txt

I changed the permissions on the script to make it executable, and then ran it. Unfortunately, I got an error on-screen that read: Word too long.

What is causing this error to occur, and how can I fix it? Thanks.
A Journey In The Quest Of Knowledge
Sandman!
Honored Contributor

Re: Outputting top information to a text file

Better run the script in debug mode and post its output for troubleshooting:

# sh -x script_name
Andrew Kaplan
Super Advisor

Re: Outputting top information to a text file

Hi there --

If I run the script with the syntax:

sh topmonitor

The script runs without a problem. It is just when I try to run it like so:

./topmonitor

That I get the error. The shell that I use is tcsh.

A Journey In The Quest Of Knowledge
Dave Hutton
Honored Contributor

Re: Outputting top information to a text file

Try adding to the top of your script
#!/usr/bin/sh

Any reason you want to use tcsh?
Sandman!
Honored Contributor

Re: Outputting top information to a text file

Well then either change your login shell to the POSIX shell "/usr/bin/sh" or run your script inside it by having... #!/usr/bin/sh ...as the first line of your script.
Dennis Handly
Acclaimed Contributor

Re: Outputting top information to a text file

>Run the top utility as sudo user, and save the results to a text file.
sudo top -f topoutput.txt

I wasn't aware that top did anything different for root?? So why do that? (All it does is make topoutput.txt have different owners and there is nothing private with the top data.)