473,586 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

output of top on a linux box

Hello,
I need the cpu usage of a linux box, for which i capture the output of
"top" using "popen". However, i am facing problems during string
handling. The code snippet is:-

top = os.popen("top -n 1")
rd = top.read().spli t("\n")
splt = rd[2].split()
# cpu = splt[2][:-4] # why ain't this working ???
cpu = splt[2]
print "cpu usage: %s, %s" % (cpu, type(cpu))

the "print" statement prints, "cpu usage: 0.5%sy, <type 'str'>". i
tried my best to get rid of the "%sy" after the "0.5", but couldn't
succeed (even though the type of "cpu" is "string").

Also, one weird thing that was happening was that if i still "split"
the cpu string (removed the last 4 characters), the output of the next
print statement was wrong (specifically, the first 2 characters got
chipped off). This output is totally unexpected, and wrong...

Can anyone kindly help me in understanding what exactly is going on here.

Thanks a lot!
Apr 9 '07 #1
0 1004

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2161
by: Fraser Gordon | last post by:
Hello, Hopefully someone can help me out with this issue... I have a python script that needs to run a shell command and be able to react to output from that command as it occurs (as opposed to waiting for the whole command to complete and then parsing the output). Is this possible with Python? I created a test script that prints a...
0
557
by: amg | last post by:
--------------2ABCD8BC51CA575D47B206C8 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit --------------2ABCD8BC51CA575D47B206C8 Content-Type: text/plain; charset=us-ascii; name="dead.mysqlbug" Content-Transfer-Encoding: 7bit
4
6761
by: Ernesto | last post by:
Hi everybody: I am developing a library using mingw (windows). I created my binary files (.dll), my library file (.a) and my def file (.def) using g++ -output-def=XXX -soname=XXX The problem happened when I ported my library to linux and tried to compile using g++. The .a and the .def file are missing because g++ did not recognize the...
20
13107
by: SR | last post by:
Hi, I need to read the output from a system( ) function within a C program. The function however only returns the exit status. How can I read what system( ) sends to stdout ? (is there a simpler way than having to fork a child process and then catching its output ?) (if needed: working on linux) Thanks
11
5186
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but the "problem" lies in the C library implementation of wcsftime()). I'm not sure if this is a platform-dependent feature or part of the C standard....
37
5041
by: nobody | last post by:
I am writing a framework that other developers will write plug-ins for. I would like for one of the features of the framework to be to intercept all text written to stdout/stderr and prepend timestamps on each line. I would like for this to work for all the printf-line functions (fprintf, etc...) as well as C++ I/O streams (cout and cerr)....
4
1649
by: mwebel | last post by:
Hi, i want to get the adress of a pointer in a string. I did so by outputting it to a ostringstream: ------------------------------ char* ptr; //generate adress in hex format std::ostringstream osstr,ossbyteSize ; //This does not work anyway //osstr.flags((std::ios_base::fmtflags)513); //cout <<osstr.flags()<<endl;
9
2616
by: cniharral | last post by:
Hi, I'm interested in printing out coloured lines of my application and I don't know what to use. Can anybody give me an idea?? Regards. Carlos Niharra López
16
4828
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables recommended for reorg. My goal is to reorgchk and run reorgs based on entries in this reorg file as shown in the example below. I have tried my hand at...
2
6595
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say select * from tablename) but when i am trying redirect output to file in Linux it shows error? can you tell me suggestion for this issue. code i...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.