The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find and replace command in one line using one command vasikaran UNIX for Dummies Questions & Answers 10 08-20-2008 07:40 AM
A utility for sending complex email messages from the command line iBot UNIX and Linux RSS News 0 06-06-2008 01:50 AM
how to? launch command with string of command line options TinCanFury Shell Programming and Scripting 5 04-28-2008 03:06 PM
command line socket read utility axes High Level Programming 3 11-20-2007 02:45 PM
Compound command with 'find' utility? deckard Shell Programming and Scripting 6 10-05-2005 01:27 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-20-2007
Registered User
 

Join Date: Nov 2007
Posts: 2
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.
Reply With Quote
Forum Sponsor
  #2  
Old 11-20-2007
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
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.
Reply With Quote
  #3  
Old 11-20-2007
Registered User
 

Join Date: Nov 2007
Posts: 2
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
Reply With Quote
  #4  
Old 11-20-2007
cassj's Avatar
Supporter
 

Join Date: Jul 2003
Location: Interweb
Posts: 107
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.
Reply With Quote
  #5  
Old 11-20-2007
assembler's Avatar
Registered User
 

Join Date: Oct 2005
Posts: 6
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.
Reply With Quote
  #6  
Old 11-20-2007
Read Only
 

Join Date: Nov 2007
Posts: 165
Curious - my post disappeared!
Reply With Quote
  #7  
Old 11-20-2007
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0