Sponsored Content
Operating Systems Linux Gentoo cpu%/mem% usage, scripting, dzen2: howto learn bash the hard way Post 302221136 by era on Sunday 3rd of August 2008 01:54:57 PM
Old 08-03-2008
For a small piece of code like that, I'm taking the liberty to quote

Quote:
Code:
#!/bin/bash

while [ 1 -eq 1 ]

The idiomatic way to code an endless loop is simply while true although you also see the obscure while : which avoids an external process (even though true is often a shell built-in in modern shells).

Quote:
Code:
do
	top -b -n 1 > /tmp/salidatop
	
	#primero cpu
	echo -n "^fg(green)CPU **"
	tail -n +8 /tmp/salidatop | sort -r -n -k9 | head -n 5 | while read line
	do 
		echo "$line" | awk ' { print "[^fg(cyan)",$12,"(^fg(red)",$9,"^fg(green)]--" } ' | tr -d ' ' | tr -d '\n'	 
	done

Is there a reason to feed awk a line at a time? Why not just

Code:
tail -n +8 /tmp/salidatop | sort -r -n -k9 | head -n 5 |
awk ' { printf "[^fg(cyan)",$12,"(^fg(red)",$9,"^fg(green)]--" } '

I don't see any spaces in the output, so tr -d ' ' seems superfluous. By using printf instead of print, there will be no trailing newline. As an aside, you could have combined the two tr:s to one: tr -d ' \n'

The same change could be applied below:

Quote:
Code:
	echo ">>"
	echo -n "^fg(green)MEM **"
	#segundo mem
	tail -n +8 /tmp/salidatop | sort -r -n -k10 | head -n 5 | while read line
	do 
		echo "$line" | awk ' { print "[^fg(cyan)",$12,"(^fg(red)",$10,"^fg(green)]--" } ' | tr -d ' ' | tr -d '\n'	 
	done

	echo ">>"
	
	sleep 5

done | dzen2 -ta l -u -l 1 -x 20 -y 710 -w 660 -e 'onstart=lower,uncollapse'

I don't understand the question about CPU percentage. You can add the user, system, and nice percentages, or just subtract the idle percentage from 100% as you note.

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).
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Difference in Mem usage ?

Hi All, I have a pair of sun ultra 5_10 with SunOS 5.5.1. Both are almost equally patched and set up with simillar applications. host# uname -a SunOS host 5.5.1 Generic_103640-24 sun4u sparc SUNW,Ultra-5_10 Even though both have same amount of RAM ( 512 Mb ) , ... (1 Reply)
Discussion started by: shibz
1 Replies

2. Linux

High Mem & Cpu Utilisation

Hi All, Kindly help me in optimizing the server as it displays a great amount of CPU & MEM being utilised when the mysql process executes. Below are the stats --- -------------------------------------------------------------------------- # top 15:51:57 up 23:22, 5 users, load average:... (1 Reply)
Discussion started by: gautamatul82
1 Replies

3. Linux

Linux Mem Usage

What is amount of free RAM i have now? total used free shared buffers cached Mem: 1010 963 46 0 215 256 -/+ buffers/cache: 491 518 Swap: 1983 0 1983 Above is the output of... (1 Reply)
Discussion started by: new2ss
1 Replies

4. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

5. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

6. Shell Programming and Scripting

Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of... (3 Replies)
Discussion started by: graysky
3 Replies

7. UNIX for Dummies Questions & Answers

HOWTO - Total memory and CPU usage ... without top?

Hi all, Is it possible to get total memory usage and free memory usage without top? By Googling I found for total memory usage, use vmstat, for CPU, use mpstat, for disk I/O use iostat, is this correct? Will using sar gives the same result as ALL of these three (3) commands? What about if I... (2 Replies)
Discussion started by: newbie_01
2 Replies

8. Shell Programming and Scripting

Should I learn bash scripting or is it going obsolete?

Hi folks, I'm a CS students enrolled in a sysadmin class where we've been covering bash scripting for the past few weeks. I have prior knowledge in java, c++, c#, python,and some scripting languages like asp.net w/c# and php. This bash stuff seems pretty neat and a bit different than what I am... (9 Replies)
Discussion started by: KalEl
9 Replies

9. Shell Programming and Scripting

Command to find total cpu and mem

Hi All, I need to know the command for knowing the total cpu and mem. Thanks (5 Replies)
Discussion started by: aish11
5 Replies

10. UNIX for Dummies Questions & Answers

Learn bash shell scripting

I do not know shell scripting. But at work place, I have got an in and out shell scripting task. I just need to understand a very big script. Is there any tool in which I can place the script and it can tell me the meaning of the whole script? (3 Replies)
Discussion started by: lg123
3 Replies
TIFF2BW(1)						      General Commands Manual							TIFF2BW(1)

NAME
tiff2bw - convert a color TIFF image to greyscale SYNOPSIS
tiff2bw [ options ] input.tif output.tif DESCRIPTION
Tiff2bw converts an RGB or Palette color TIFF image to a greyscale image by combining percentages of the red, green, and blue channels. By default, output samples are created by taking 28% of the red channel, 59% of the green channel, and 11% of the blue channel. To alter these percentages, the -R, -G, and -B options may be used. OPTIONS
-c Specify a compression scheme to use when writing image data: -c none for no compression, -c packbits for the PackBits compression algorithm, -c zip for the Deflate compression algorithm, -c g3 for the CCITT Group 3 compression algorithm, -c g4 for the CCITT Group 4 compression algorithm, and -c lzw for Lempel-Ziv & Welch (the default). -r Write data with a specified number of rows per strip; by default the number of rows/strip is selected so that each strip is approxi- mately 8 kilobytes. -R Specify the percentage of the red channel to use (default 28). -G Specify the percentage of the green channel to use (default 59). -B Specify the percentage of the blue channel to use (default 11). SEE ALSO
pal2rgb(1), tiffinfo(1), tiffcp(1), tiffmedian(1), libtiff(3) October 15, 1995 TIFF2BW(1)
All times are GMT -4. The time now is 02:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy