![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Gentoo Gentoo Linux is a versatile and fast, completely free Linux distribution geared towards developers and network professionals. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help- To learn shell scripting | tj23 | Shell Programming and Scripting | 2 | 06-20-2008 06:10 AM |
| newbie: way to learn more about server's resource usage | blakekr | UNIX for Dummies Questions & Answers | 2 | 05-19-2008 11:43 PM |
| Hard disk usage is 100% Busy for any command | npcrao | AIX | 3 | 11-14-2007 06:18 AM |
| Programming/Scripting Languages To Learn | tjinr | UNIX for Dummies Questions & Answers | 1 | 07-24-2006 12:32 PM |
| What is a good place to learn basic shell scripting? Thanks! | deutchap6verse5 | Shell Programming and Scripting | 2 | 03-08-2005 02:55 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
|||||
|
cpu%/mem% usage, scripting, dzen2: howto learn bash the hard way
I am trying to write a small (and rather simple) script to gather some info about the system and piping it to dzen2
first, i want to explain some things. I know i could have used conky, but my intention was to expand my knowledge of bash, pipes and redirections inside a script, and to have fun (which i am) some time later, i also decided to try to be more strict on the resource usage. this is a script that will constantly run on a low-end laptop. also, dzen is not the important part of the script and can be simple ignored, is just a way to show the info. if you are interested in dzen, you can read more info here gotmor - dzen so, lets talk about the script itself i use top in batch mode to get the list of most cpu/mem using, but now, i would like to include the total too. (in percentage) with the memory, i was thinking maybe using free, and doing some math on the used/cached fields. but that would involve a new app to run, when top already gave me the info. but taking the info from top's output will require a heavy use of pipes and secondary apps like tail and head with the cpu%, well, im lost ..... where could i find that info? im thinking, maybe is better to just use the iddle% field of top this is what i have done so far general pastebin - broli - post number 1091305 any suggestion on how to improve what i have done will also be welcomed |
|
||||
|
For a small piece of code like that, I'm taking the liberty to quote
Quote:
Quote:
Code:
tail -n +8 /tmp/salidatop | sort -r -n -k9 | head -n 5 |
awk ' { printf "[^fg(cyan)",$12,"(^fg(red)",$9,"^fg(green)]--" } '
The same change could be applied below: Quote:
If you can replace the complex tail | head etc with a simple sed or awk script, that will probably help reduce the resource requirements. Perhaps you could pass some option to top to order the output like you want it, so you can avoid the separate sort -- that's probably the main bottleneck here (albeit a very minor one, with so little input). |
|
||||
|
Obiously, awk is a fairly complete programming language, so you can rewrite simple text-processing utilities in awk, many of them easily.
Code:
awk 'NR==10 { exit 0 }1' # head -10
awk '{ print $2 }' # cut -f2 (splitting on runs of whitespace though)
See also http://www.pement.org/awk/awk1line.txt The first few lines of top output indicate the CPU usage. Read the manual page to learn what the fields mean. Code:
Cpu(s): 16.8%us, 1.7%sy, 0.0%ni, 80.5%id, 0.8%wa, 0.1%hi, 0.1%si, 0.0%st |
|
|||||
|
Quote:
what about sort? i must admint my knowledge of awk is fairly simple. i ave seen more complex ones, and im reading this website AWK Language Programming - Table of Contents readnig the temporal file with awk and doing all the text procesing will decrease the number of binaries that have to be loaded (less i/o) Quote:
and perl has always been in my todo-list the problem there would be to pipe the info to dzen because the way dzen works, i need an never ending loop, so dzen dosnt die is posible to do that piping from a perl script? Quote:
Quote:
i know that. the problem is getting that info without using head | tail | cut or awk spawning all those apps 4 times on the same text input file doesnt strike me as good performance thanks for the replies ! |
|
||||
|
Quote:
Quote:
But the requirements you have shown so far should be easy to handle in awk; it's just not clear from reading the code in which direction you want the script to grow, and/or I was too lazy to rewrite it all. |
|
|||||
|
Quote:
Quote:
"moving to perl" doesn't mean anything "bad" i will finish this with bash (because i wont let this sucker win). and after that, i will try to make it in perl. just because i can (or rather "just because i still dont know how" ) Quote:
for the cpu. i had some doubts, but i have decided to show the iddle percentage. this is a personal thing, i will use it, so if i want/know iddle%, thats what im gonna get :P for the memory, i have been researching. i was using wmmem, so i went and read the code to and read how it calculated. i came up with the math behind it. ((total - cache - buffer) * 100 ) / total now my problem is getting those 3 from top unfortunately, all my work is in my personal laptop (at home), and usually, i dont spent time in this during the week, so i cant give you any particulars on what i have done so far to retrieve that data i dont ask you to rewrite my script. actually i would hate that. as i said, i want to solve this, or at least try thanks for the great help so far ! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|