0

I want to retrieve percentage processor usage but no other commands that show tons of numbers. There's a solution using 'grep' or 'awk' or something like that.

~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0 1447984 190232 146508 4983236  0    0     0    10    0    0  2  1 97  0

I used the vmstat command, and I only want the 'sy' number in the cpu colon.

top command output :
    [m20176 libvirt-  20   0 4368m 4,0g 3028 S  12,0 25,3  24695:44 kvm               (B[m[39;49m

(B[m25320 libvirt-  20   0 3520m 3,0g 3056 S   2,7 19,2  21786:15 kvm               (B[m[39;49m

(B[m    1 root      20   0 10656  624  596 S   0,0  0,0   5:46.26 init              (B[m[39;49m
(B[m    2 root      20   0     0    0    0 S   0,0  0,0   0:00.12 kthreadd          (B[m[39;49m
(B[m    3 root      20   0     0    0    0 S   0,0  0,0 476:10.20 ksoftirqd/0       (B[m[39;49m
(B[m    6 root      rt   0     0    0    0 S   0,0  0,0   0:08.16 migration/0       (B[m[39;49m
(B[m    7 root      rt   0     0    0    0 S   0,0  0,0   2:03.06 watchdog/0        (B[m[39;49m
(B[m    8 root      rt   0     0    0    0 S   0,0  0,0   0:04.30 migration/1       (B[m[39;49m
(B[m   10 root      20   0     0    0    0 S   0,0  0,0   0:38.83 ksoftirqd/1       (B[m[39;49m
(B[m   12 root      rt   0     0    0    0 S   0,0  0,0   1:43.93 watchdog/1        (B[m[39;49m
(B[m   13 root      rt   0     0    0    0 S   0,0  0,0   0:03.41 migration/2       (B[m[39;49m
(B[m   15 root      20   0     0    0    0 S   0,0  0,0   2:42.22 ksoftirqd/2       (B[m[39;49m
(B[m   16 root      rt   0     0    0    0 S   0,0  0,0   1:49.23 watchdog/2        (B[m[39;49m
(B[m   17 root      rt   0     0    0    0 S   0,0  0,0   0:04.42 migration/3       (B[m[39;49m
(B[m   19 root      20   0     0    0    0 S   0,0  0,0 408:06.08 ksoftirqd/3       (B[m[39;49m

Here, process uses per process are displayed (only a part). I found this command :

`top -b -d1 -n1|grep -i "Cpu(s)"|head -c21|cut -d ' ' -f3|cut -d '%' -f1`

here. But this is CPU usage from startup, not real-time usage.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Jeulis
  • 31
  • 10
  • 1
    Please add your efforts too in your post, what you tried to solve this? – RavinderSingh13 Jun 18 '18 at 12:44
  • 1
    Possible duplicate of [How to get overall CPU Usage (e.g. 57%) on Linux](https://stackoverflow.com/q/9229333/608639), [How can I determine the current CPU utilization from the shell?](https://stackoverflow.com/q/1332861/608639), [How to get percentage of processor use with bash?](https://stackoverflow.com/q/26791240/608639), [Get the load, cpu usage and time of executing a bash script](https://stackoverflow.com/q/26425154/608639), etc. – jww Jun 18 '18 at 15:49

1 Answers1

3

A simple awk could help you here(considering that you want to print only the numbers of sy column).

vmstat 1 10 | awk 'FNR>1{print $(NF-3)}'

NOTE: I have used vmstat 1 10 to perform 10 times vmstat command on server and then I am printing the $(NF-3) value which is 4th value from last.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 2
    Thanks so much, it solved my problem with some edit : `vmstat 1 2 | awk 'FNR>3{print $(NF-3)}'` to get only one number and deleting the "sys" word – Jeulis Jun 18 '18 at 13:08