Skip to content
Advertisement

CPU and HDD information

I searched but I found nothing for my problem. I would like to have in Linux command line the information about the CPU usage and the local HDDs with formatting text like exactly as the examples below for my program. These examples are command line outputs on MS-Windows. I hope it is possible on Linux, too.

Thank you

wmic logicaldisk where drivetype=3 get caption,freespace,size

Caption  FreeSpace     Size
C:       135314194432  255953203200
D:       126288519168  128033222656
E:       336546639872  1000194015232
F:       162184503296  1000194015232

wmic cpu get loadpercentage

LoadPercentage
4

Advertisement

Answer

There is no command that gives you a load percentage of the cpu. It’s actually impossible to get that with a system call (nor in linux neither in Windows). What you can get is the number of ticks currently executed (for each field, user, system, io,irq idle)…, then call it again a certain amount of time later and calculate it. That way is how work all the commands for reading the cpu percentage.

Here a script bash that gives you that: (just create a file named for example cpu.sh paste this code and execute to see the results)

_estado()
{
      cat /proc/stat | grep "cpu " |  sed -e 's/  */:/g' -e 's/^cpux//'
}

_ticksconcretos()
{
      echo $1 | cut -d ':' -f $2 

}

while true ; do
      INICIAL=$(_estado)
      sleep 1
      FINAL=$(_estado)

      UsuarioI=$(_ticksconcretos $INICIAL 1)
      UsuarioF=$(_ticksconcretos $FINAL 1)

      NiceI=$(_ticksconcretos $INICIAL 2)
      NiceF=$(_ticksconcretos $FINAL 2)

      SistemaI=$(_ticksconcretos $INICIAL 3)
      SistemaF=$(_ticksconcretos $FINAL 3)

      idleI=$(_ticksconcretos $INICIAL 4)
      idleF=$(_ticksconcretos $FINAL 4) 

      IOI=$(_ticksconcretos $INICIAL 5)
      IOF=$(_ticksconcretos $FINAL 5)

      IRQI=$(_ticksconcretos $INICIAL 6)
      IRQF=$(_ticksconcretos $FINAL 6)

      SOFTIRQI=$(_ticksconcretos $INICIAL 7)
      SOFTIRQF=$(_ticksconcretos $FINAL 7)   

      STEALI=$(_ticksconcretos $INICIAL 8)
      STEALF=$(_ticksconcretos $FINAL 8)   

      InactivoF=$(( $idleF + $IOF ))
      InactivoI=$(( $idleI + $IOI ))
      ActivoI=$(( $UsuarioI + $NiceI + $SistemaI + $IRQI + $SOFTIRQI + $STEALI ))
      ActivoF=$(( $UsuarioF + $NiceF + $SistemaF + $IRQF + $SOFTIRQF + $STEALF ))

      TOTALI=$(( $ActivoI + $InactivoI ))
      TOTALF=$(( $ActivoF + $InactivoF ))

      PORC=$(( ( ( ( $TOTALF - $TOTALI ) - ( $InactivoF - $InactivoI ) ) * 100 / ( $TOTALF - $TOTALI ) ) ))
      clear
      echo "CPU:  $PORC %"

      done

For the free space You could use something like this:

df -h  -x tmpfs -x devtmpfs | awk -F " " '{print $1 " " $4 " " $2}'

wich will give you this output:

Filesystem Free Size
/dev/sda1 16G 25G
/dev/sda5 46G 79G
/dev/sdb8 130G 423G

sda represents the first disk, sda1 the first partition, sda2, the second one etc. you can add (or change) $6 inside the print to get the mount points instead of the partitions, change the order or even more things.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement