Tiny Core Linux

Tiny Core Base => TCB Q&A Forum => Topic started by: wolf_core on August 26, 2012, 02:15:23 PM

Title: Show CPU usage
Post by: wolf_core on August 26, 2012, 02:15:23 PM
Hi, i have a script that use the code below to show cpu usage in a web page, the code is write for arch linux, how can i modify the code to extract  the CPU data usage in Tiny Core?

Code: [Select]
$CPUUSE="ps aux|awk 'NR > 0 { s +=$3 }; END {print \"cpu %\",s}' | awk '{ print $3 }'";
Thanks in advance Luca
Title: Re: Show CPU usage
Post by: tinypoodle on August 26, 2012, 05:13:10 PM
You might want to look into procps or GNU ps, as busybox ps does not provide cpu output.
Title: Re: Show CPU usage
Post by: curaga on August 26, 2012, 05:37:55 PM
Maybe grab it from top?
Title: Re: Show CPU usage
Post by: ananix on August 27, 2012, 01:03:10 PM
good surgestion curaga
$top -n 1 | grep CPU: | grep -v grep | awk ' { print $1, $8, $9 } '

wondering how you tend to use the VAR maybe you can save a step by using cmd substitute (`) instead of quoting (") depending on what you do afterwards with the VAR
Title: Re: Show CPU usage
Post by: Rich on August 27, 2012, 01:27:50 PM
Hi ananix
I think will work:
Code: [Select]
$CPUUSE=echo `top -n 1 | grep CPU: | grep -v grep | awk ' { print $1, $8, $9 } '`Or you might have to move the first  `  in front of  echo
Title: Re: Show CPU usage
Post by: ananix on August 27, 2012, 01:45:11 PM
Hi Rich i meant if he just needs a VAR with the result he could use
VAR=`top -n 1 | grep CPU: | grep -v grep | awk ' { print $1, $8, $9 } '`
Another consideration, one should also use head and tail instead when knowing line number to avoid miss graps of CPU:
Title: Re: Show CPU usage
Post by: wolf_core on August 27, 2012, 06:01:18 PM
Thanks everybody, i have try with all the solutions shows upside, but no data is shown in the web page. The best data output is xx.x% obtained with the code below but i think that the script use a data xx.x without % sign.
Now I have asked to  the script  the author how the $CPUUSE should be valorized to show data in the web page.

Code: [Select]
$CPUUSE=`top -n 1 | grep CPU: | grep -v grep | awk ' { print $2 } '`
Thanks Luca
Title: Re: Show CPU usage
Post by: tinypoodle on August 27, 2012, 07:27:06 PM
Agreed, preferable if busybox's top can be used rather than procps or GNU ps.
Title: Re: Show CPU usage
Post by: curaga on August 28, 2012, 05:40:17 AM
The percent sign is easy to remove, use sed for example:

Code: [Select]
echo '30%' | sed 's@%@@g'
Title: Re: Show CPU usage
Post by: wolf_core on August 28, 2012, 08:00:47 AM
Thanks curaga I will try

Code: [Select]
$CPUUSE="(float)`top -n 1 | grep CPU: | grep -v grep | awk ' { print $2 } ' | sed 's@%@@g'`" ;
the script author say
Quote
that $CPUUSE, should output a float number without any % sign.

Thanks Luca