WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Show CPU usage  (Read 5435 times)

Offline wolf_core

  • Jr. Member
  • **
  • Posts: 69
Show CPU usage
« 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

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: Show CPU usage
« Reply #1 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.
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11041
Re: Show CPU usage
« Reply #2 on: August 26, 2012, 05:37:55 PM »
Maybe grab it from top?
The only barriers that can stop you are the ones you create yourself.

Offline ananix

  • Full Member
  • ***
  • Posts: 174
Re: Show CPU usage
« Reply #3 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
« Last Edit: August 27, 2012, 01:06:57 PM by ananix »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11576
Re: Show CPU usage
« Reply #4 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

Offline ananix

  • Full Member
  • ***
  • Posts: 174
Re: Show CPU usage
« Reply #5 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:

Offline wolf_core

  • Jr. Member
  • **
  • Posts: 69
Re: Show CPU usage
« Reply #6 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

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: Show CPU usage
« Reply #7 on: August 27, 2012, 07:27:06 PM »
Agreed, preferable if busybox's top can be used rather than procps or GNU ps.
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11041
Re: Show CPU usage
« Reply #8 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'
The only barriers that can stop you are the ones you create yourself.

Offline wolf_core

  • Jr. Member
  • **
  • Posts: 69
Re: Show CPU usage
« Reply #9 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