Your script could be improved in several ways. Here's three.
Quote:
Originally Posted by Toushi
1) Every time the top command out put getting different out put.
|
Most importantly: use temporary files when you need to (and you do).
Code:
nice -n -20 $$; export TMPDIR=/dev/shm
_MYTMPDIR=`mktemp -p /dev/shm -d progn.XXXXXXXXXX` && {
generate_output0 > "${_MYTMPDIR}/somefile0" &
generate_output1 > "${_MYTMPDIR}/somefile1" &
wait_for_exit; process_output; rm -rf "${_MYTMPDIR}"
Storing data this way, having all commands produce output at almost the same time, ensures you get all information for that time "snapshot".
Quote:
Originally Posted by Toushi
echo "Current CPU utilization is:"
|
Secondly prioritize. Separate "must haves" from "nice to have". Getting accurate data is, easier correlation is but pretty-printing
is not the most important. And when you're pretty-printing why make it take up so much gawddarn space? The whole
Code:
echo
echo "Current CPU utilization is:"
echo "--------------------------------------------------"
echo "$CPUUSAGE"
echo
and more could be efficiently written as
Code:
echo -en "CPU Loadavg mem IO\n${CPUUSAGE} ${LOADAVG} ${MEMINFO} ${IODETAIL}\n"
Quote:
Originally Posted by Toushi
LOADAVG=`top -n1 |head -1 | awk '{print $10, $11, $12, $13, $14}'`
|
Userland tool output can differ due to OS and application versions (unless portability isn't an issue) and can be influenced in several ways: environment variables, resource files and command line switches. So apart from some making the case for setting default behaviour always ("
LANG=C; LC_ALL=C; export LANG LC_ALL") you should get to know your tools. Unless you configure 'top' using (
a temporary!) ~/.toprc you'll be provided with more and varying information than you need resulting in longer execution time, a longer and potentially more difficult processing chain meaning more tool execution time. And you don't need 'top' in all cases. For instance returning all UID's could be as easy as
Code:
/bin/ps ax -o uid --noheaders
and returning all processes and only displaying actual CPU utilization and UID's could be done with
Code:
/bin/ps ax -eo pcpu,uid --noheaders|grep -v '^[[:blank:]]0.0'
(Note
any conversion takes up time so while having local user names sounds nice it comes at a cost. Easier to 'getent passwd $UID' when you actually need it.)
I know, all of this sounds a bit like it's "too much" for a simple script but IMHO it's the thought process that counts. If you don't dig that just wait until you need to add details, run your script say once a second on a burdened machine or have somebody ask you to provide a portable version.
*One more thing. For some reinventing the wheel is compulsory, for others compulsive and for others completely unnecessary because lots of solutions are already available from Sourceforge, The-Site-Formerly-Known_As-Freshmeat, Nongnu, Berlioz, your distributions or 3rd party documentation, forum and web log posts. Sometimes being able to use somebody elses work or adapt something may be more efficient.