You are not logged in.

#1 2019-07-10 15:15:47

loutch
Member
Registered: 2015-12-12
Posts: 311

equivalent "cpu cpu0 " for console

Hello

i try to found any command line  for terminal equivalent as {cpu cpu0} in conky  to have cpu charge in a console &

to try it in a script


@+


Linuxmint 19.1 Tessa cinnamon & mageia 7 mate on ssd hp pavilion g7
Xubuntu 18.10 xfce on ASUS Rog STRIX

Offline

#2 2019-07-10 16:04:06

unklar
Member
Registered: 2015-10-31
Posts: 884

Re: equivalent "cpu cpu0 " for console

Maybe?

apt policy sysstat
sysstat:
  Installiert:           (keine)
  Installationskandidat: 11.4.3-2
  Versionstabelle:
     11.4.3-2 500
        500 https://deb.debian.org/debian stretch/main amd64 Packages

Source

Offline

#3 2019-07-10 16:43:10

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,431

Re: equivalent "cpu cpu0 " for console

Off the top of my head you could start with

# Subtract the idle% from 100% to get the cpu usage%.
vmstat -n 1 2 | awk 'NR==4{print(100-$15"%");}'

unfortunately since vmstat displays the data needed only after the first line after a sample period of >=1 seconds, it takes 1s to run which is far from instantaneous.


What can you see / On the horizon?

Offline

#4 2019-07-10 17:02:10

PackRat
jgmenu user Numero Uno
Registered: 2015-10-02
Posts: 1,056

Re: equivalent "cpu cpu0 " for console

mpstat - which I think is part of package of utilities in Debian; maybe sysstat.

It's used for the default cpu_usage script for i3blocks:

#!/usr/bin/perl
#
# Copyright 2014 Pierre Mavro <deimos@deimos.fr>
# Copyright 2014 Vivien Didelot <vivien@didelot.org>
# Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com>
#
# Licensed under the terms of the GNU GPL v3, or any later version.

use strict;
use warnings;
use utf8;
use Getopt::Long;

# default values
my $t_warn = 50;
my $t_crit = 80;
my $cpu_usage = -1;

sub help {
    print "Usage: cpu_usage [-w <warning>] [-c <critical>]\n";
    print "-w <percent>: warning threshold to become yellow\n";
    print "-c <percent>: critical threshold to become red\n";
    exit 0;
}

GetOptions("help|h" => \&help,
           "w=i"    => \$t_warn,
           "c=i"    => \$t_crit);

# Get CPU usage
$ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is
open (MPSTAT, 'mpstat 1 1 |') or die;
while (<MPSTAT>) {
    if (/^.*\s+(\d+\.\d+)\s+$/) {
        $cpu_usage = 100 - $1; # 100% - %idle
        last;
    }
}
close(MPSTAT);

$cpu_usage eq -1 and die 'Can\'t find CPU information';

# Print short_text, full_text
printf "%.2f%%\n", $cpu_usage;
printf "%.2f%%\n", $cpu_usage;

# Print color, if needed
if ($cpu_usage >= $t_crit) {
    print "#FF0000\n";
    exit 33;
} elsif ($cpu_usage >= $t_warn) {
    print "#FFFC00\n";
}

exit 0;

You can find some bash scripts using it if you check out the i3, dwm (tiling wm's in general) threads and see how their status bars are set up.

There is also the psutil suite if you want to go with python.

Last edited by PackRat (2019-07-10 17:04:33)


You must unlearn what you have learned.
    -- yoda

Online

#5 2019-07-11 06:59:23

loutch
Member
Registered: 2015-12-12
Posts: 311

Re: equivalent "cpu cpu0 " for console

hello

I’m trying to get this        1562828306.png   out of the terminal with this commande

top -n1 | grep 'Cpu(s)'| awk '{print $2;}' | sed -e 's/id,//g'

that donne this

~/Bureau$ top -n1 | grep 'Cpu(s)'| awk '{print $2;}' | sed -e 's/id,//g'
0,9

i would just the number(s) before the ,

Last edited by loutch (2019-07-11 07:06:48)


Linuxmint 19.1 Tessa cinnamon & mageia 7 mate on ssd hp pavilion g7
Xubuntu 18.10 xfce on ASUS Rog STRIX

Offline

#6 2019-07-11 08:08:22

Naik
Member
From: Lipsia
Registered: 2015-10-03
Posts: 168

Re: equivalent "cpu cpu0 " for console

how about this?

USER@HOST[~]$ top -n1 | grep 'Cpu(s)'| awk '{print $2;}' | cut -c1-1
5

naik --greetz


"Kaum macht [Mensch]* es richtig, funktioniert es sofort!"
BL-Kitchen on GitHub

Offline

#7 2019-07-11 08:30:27

earlybird
ほやほや
Registered: 2015-12-16
Posts: 701
Website

Re: equivalent "cpu cpu0 " for console

Naik wrote:

how about this?

USER@HOST[~]$ top -n1 | grep 'Cpu(s)'| awk '{print $2;}' | cut -c1-1
5

naik --greetz

top's layout is configurable (esp. since the default layout of top changed a while ago) which is why the output of top -n1 may not be predictable. At least you'd need to ignore toprc to get a solution that works not on just your computer (I can't find a switch that does it?).

Offline

#8 2019-07-11 09:04:43

loutch
Member
Registered: 2015-12-12
Posts: 311

Re: equivalent "cpu cpu0 " for console

Naik wrote:

how about this?

USER@HOST[~]$ top -n1 | grep 'Cpu(s)'| awk '{print $2;}' | cut -c1-1
5

naik --greetz

i try the cut -c1-2 ( done 7,  work great with number who begin with 10 ) & c1-3 (done 8,1 or 12,)

Last edited by loutch (2019-07-11 09:09:29)


Linuxmint 19.1 Tessa cinnamon & mageia 7 mate on ssd hp pavilion g7
Xubuntu 18.10 xfce on ASUS Rog STRIX

Offline

#9 2019-07-11 11:47:27

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,769

Re: equivalent "cpu cpu0 " for console

I have this

# cpu usage in percents 
tmp=$(ps -A -o pcpu | tail -n+2 | paste -sd+ | bc)
cpu=$(awk_round 0 "$tmp")
echo "$cpu% cpu used"

Offline

Board footer

Powered by FluxBB