Get CPU usage via SSH
17,013
Solution 1
Thanks everyone but I managed. I did this command:
top -b -n 10 -d.2 | grep 'Cpu' | awk 'NR==3{ print($2)}'
Solution 2
you can use
top -n 1
mpstat
iostat
Solution 3
Even better would be to simply install glances.
sudo apt install glances
Then simply run glances
. Then your output would look like this. You can change the update speed.
$yourhostname - IP x.x.x.x/x Pub xx.xxx.xxx.xxx Uptime: 0:41:24
CPU [ 1.4%] CPU - 1.4% MEM - 10.8% SWAP - 0.0% LOAD 12-core
MEM [ 10.7%] user: 1.0% total: 31.4G total: 2.00G 1 min: 0.71
SWAP [ 0.0%] system: 0.4% used: 3.38G used: 0 5 min: 0.73
idle: 98.5% free: 28.0G free: 2.00G 15 min: 0.69
NETWORK Rx/s Tx/s TASKS 322 (1136 thr), 1 run, 235 slp, 86 oth
eno1 0b 0b
lo 2Kb 2Kb Systemd 6 Services loaded: 190 active: 190
_xxxxxxxx 0b 4Kb
CPU% MEM% PID USER NI S Command
DefaultGateway 62ms 4.2 0.9 1923 xxx 0 S cinnamon --replace
2.9 0.1 9055 xxx 0 R /usr/bin/python3 /
DISK I/O R/s W/s 1.9 0.5 1323 root 0 S /usr/lib/xorg/Xorg
disk1 0 91K 1.3 0.0 1338 root 0 S irq/74-nvidia
partition1 0 0 1.3 0.1 8586 xxx 0 S /usr/lib/gnome-ter
partition2 0 91K 1.0 0.4 8907 xxx 0 S /opt/google/chrome
sda1 0 0 0.6 1.0 5381 xxx 0 S /home/xxx/Programs
sdc1 0 0 0.3 0.5 2102 xxx 0 S /opt/google/chrome
0.3 0.4 2689 xxx 0 S /opt/google/chrome
FILE SYS Used Total 0.3 0.6 4831 xxx 0 S /home/xxx/Programs
/ 15.6G 228G
2019-02-13 05:39:04 No warning or critical alert detected
Related videos on Youtube
Author by
user3120926
Updated on June 04, 2022Comments
-
user3120926 almost 2 years
I want to retrieve the CPU utilization percentage via SSH and I have tried the command "top" but it will not let me.
I am using CentOS 6.
I tried this code
$connection = ssh2_connect("IP", PORT); ssh2_auth_password($connection, "root", "PASS"); $stream = ssh2_exec($connection, "top"); $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); // Enable blocking for both streams stream_set_blocking($errorStream, true); stream_set_blocking($stream, true); // Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothing echo "Output: " . stream_get_contents($stream); echo "Error: " . stream_get_contents($errorStream); // Close the streams fclose($errorStream); fclose($stream);
But its every time give me an error: Output: Error: TERM environment variable not set.
I'm using PHP.
-
TypeIA over 10 yearsYou'll probably need to give some details on what "it will not let me" means... like an error message or something...
-
Sammitch
top
assumes it's running from an interactive shell by default, trytop -bn 1
. Alsoman top
.
-