top command line utility


 
Thread Tools Search this Thread
Top Forums Programming top command line utility
# 1  
Old 11-20-2007
top command line utility

I'm writing a monitoring application. I'd like to periodically get the information provided by the 'top' command line utility from within my code and write the output of 'top' to a file. Wondering if anyone has already done something like this.
Doing
system("top > someFile");
does not create the file since it depends on the terminal.
# 2  
Old 11-20-2007
What information are you trying to get? What platform and Unix are you using? Running a curses-based app that imposes a measurable amount of load on it's own doesn't sound optimal to me - perhaps there's a way to avoid using 'top -b' to get what you want.
# 3  
Old 11-20-2007
We have a robotics app that is dying. It's usually because a thread is in a tight loop. So I'd like to get cpu usage info. I've been looking at the top code and trying to extract the actual reading of the system info. It's mainly screen handling code. I'm actually building a framework for monitoring real time systems. This app is a small plug in component to that framework.

thanks
# 4  
Old 11-20-2007
This worked for me to get a top snapshot (I'm running bash in Ubuntu 7.10):

top -b -n 1 > ~/Desktop/top.txt


I did a man top to find out that:

-b : Batch mode operation
Starts top in ’Batch mode’, which could be useful for sending output from top to other programs or
to a file. In this mode, top will not accept input and runs until the iterations limit you’ve set
with the ’-n’ command-line option or until killed.

-n : Number of iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top should produce before ending.
# 5  
Old 11-20-2007
I have OpenSuse 10.2 installed and I tried top command like this:

top -b -n 5 -d 3.0 -H > ~/Desktop/top_info.txt

-b - Batch mode
-n - how many itterations
-d - delay of each of each output
-H - out the threads

It works on my PC perfectly.
# 6  
Old 11-20-2007
Curious - my post disappeared!
# 7  
Old 11-20-2007
Well, if you insist on using top, double-check the man page. I found this in mine (shipped with GNU procps-3.2.7-8.1.el5):
Quote:
7. BUGS
Send bug reports to:
Albert D. Cahalan, <albert@users.sf.net>

The top command calculates Cpu(s) by looking at the change in CPU time values between samples. When
you first run it, it has no previous sample to compare to, so these initial values are the percent-
ages since boot. It means you need at least two loops or you have to ignore summary output from the
first loop. This is problem for example for batch mode. There is a possible workaround if you define
the CPULOOP=1 environment variable. The top command will be run one extra hidden loop for CPU data
before standard output.
Plus, rather than incur an even heavier load by invoking top, you could do as top does to get it's information; in my case it reads /proc/stat to get the CPU counters. Since you didn't reply with your OS, I don't know if this will work for you.

BTW, I'm not sure if you mean RTOS, but if you really need this to be real-time, I'd definitely minimize the impact of monitoring as much as possible.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue in running a command line utility in CRON

Hi Everyone! I am facing an issue in running a command line utility from the CRON. This utility displays IPC statistics on UNIX message queues: The "queue name" and the "count" of messages in the queue. When running this utility from prompt, it will provide an output on the screen, like the... (4 Replies)
Discussion started by: vai_sh
4 Replies

2. Shell Programming and Scripting

Top utility in bash for loop

Dear All, is it possible to use top utility in for loop? I am trying to get top swap space consuming processes in my for loop. I got multiple way (using /proc FS) however have anybody used top command in for loop? Using /proc FS giving solutionbut itrs more complex. Please suggest if... (4 Replies)
Discussion started by: mail2vivek1
4 Replies

3. UNIX for Dummies Questions & Answers

FTP command-line utility usage

Hi, Using command-line utility "ftp or sftp", I want to transfer files across Windows and UNIX. Can you please tell me from where I need to connect to ftp and how do I specify the hostname, credentials and how do I get and put files between DOS and UNIX? Please provide me as much... (10 Replies)
Discussion started by: Dev_Dev
10 Replies

4. Shell Programming and Scripting

DB Access Command Line Utility

To read/write to a DB from Java or Perl, you usually have to install/reference several drivers and write a whole bunch of boilerplate DB access code. I'm curious if someone has written a command line utility for Unix/Linux for simple database access for the major providers, something like: ... (3 Replies)
Discussion started by: furashgf
3 Replies

5. UNIX Desktop Questions & Answers

How do you reverse terminal command line to the top?

Hi All, I work on a Linux platform which runs Red Hat (forget which version) and use both korn and bash shells. Is there a way of making the command line appear at the top of the terminal window and any lists, commands or directory names etc to appear below the top, that is to say reverse the... (1 Reply)
Discussion started by: ray_m
1 Replies

6. Linux

The dot command-line utility?

Hi, What else is the dot used beside relative filepaths in bash? Is it a shell utility as well? No man entry for dot (.)... (3 Replies)
Discussion started by: varelg
3 Replies

7. Programming

command line socket read utility

HI I have a messaging s/w daemon(TIBCO rvrd) provided by vendor which will accept connections from various clients and routes messages to the destinations. In order to route it internally uses two ports(one tcp adn one udp). I want to know on which port(tcp/udp) it is transmitting... (3 Replies)
Discussion started by: axes
3 Replies

8. Shell Programming and Scripting

how can i extract only the Memory line from top command ?

Hello all i need in csh to extract only the Memory line from the out put of the top command how can it easily done (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question