In linux, I'm writting a script to log system parameters to a file.
How can I get the name of the task consuming the most CPU resources, and the percentage of CPU used by that task?
For example, using top:
$ top -bin 1
top - 19:11:05 up 2:57, 1 user, load average: 1,43, 1,47, 1,06
Tasks: 178 total, 2 running, 124 sleeping, 0 stopped, 0 zombie
%Cpu(s): 5,8 us, 1,3 sy, 0,0 ni, 92,8 id, 0,0 wa, 0,0 hi, 0,1 si, 0,0 st
KiB Mem : 3892704 total, 1594348 free, 1282992 used, 1015364 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 2335136 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11883 root 20 0 645964 104036 87792 R 93,8 2,7 18:07.03 Xorg
12030 raf 20 0 412824 35632 14860 S 12,5 0,9 2:44.51 xfsettingsd
23468 raf 20 0 39648 3864 3332 R 6,2 0,1 0:00.02 top
From the exammple above, what I would like to have is a [sequence of [piped]] bash command[s] that outputs:
93.8 Xorg
You can try
ps -eo %cpu,comm --sort %cpu | tail -n 1
Related
Following top-table is given:
With following command:
top -bn1p 20101
I get following result:
top - 11:38:34 up 248 days, 1:17, 3 users, load average: 0,09, 0,16, 0,18
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1,8 us, 0,9 sy, 0,0 ni, 97,2 id, 0,0 wa, 0,0 hi, 0,0 si, 0,0 st
KiB Mem: 24693872 total, 24430392 used, 263480 free, 142532 buffers
KiB Swap: 15625212 total, 17508 used, 15607704 free. 12526360 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
20101 root 20 0 11,636g 262944 18260 S 0,0 1,1 88:13.84 java
Like you see CPU-Value is 0.0 . Why? It should be 0.3.
Is this a bug?
I have this script:
echo $(date +%F-%H%M ) $( top -n 1 -b -c -p $ZK_PID,$KAFKA_PID,$AGENT_PID,$ENGINE_PID | tail -n 1) >> `hostname`_top.log
which produce the following output:
top - 06:32:15 up 7 days, 21:22, 2 users, load average: 1.71, 1.66, 1.66
Tasks: 3 total, 0 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu(s): 22.8 us, 15.9 sy, 0.0 ni, 61.1 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 14360876 total, 191296 free, 10837496 used, 3332084 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 3066536 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
48721 equalum 20 0 12.828g 1.214g 6756 S 3.0 8.9 2176:15 /usr/lib/+
52019 equalum 20 0 5809096 1.436g 5392 S 1.3 10.5 450:51.78 java -Dna+
48411 equalum 20 0 4150868 403536 4992 S 0.0 2.8 3:56.87 /usr/lib/+
I am trying to get only %CPU and %MEM values for those processes , how can i do that?
Try to add this after your script code
| grep '%CPU|%MEM'
I have done memory test on my cloud instance with Sysbech utility.
Using following command:
sysbench --test=memory --memory-block-size=1K --memory-scope=global --memory-total-size=100G --memory-oper=write run
During the Run:
TOP command result
I never found Mem % is increased.
but CPU usage increased upto 100%
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5664 root 20 0 78036 2704 1908 S 99.5 0.4 0:09.42 sysbench
1 root 20 0 19276 500 348 S 0.0 0.1 0:00.81 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
is it strange ?
What does Memory test in Sysbench actually mean ?
I am trying to capture the CPU idle time from TOP.
The following code captures Load Average
I am trying to manipulate the following code so that it capture's CPU idle time.
Any ideas welcome.
top -bn1 | grep load | awk '{printf "CPU load %: %.2f\n", $(NF-2)}'
The Above Code Outputs: CPU load %: 0.44
I want to change the code so that it outputs CPU idle time
CPU Id %: 92.9%
Example Top output:
top - 10:35:25 up 1 day, 16:06, 5 users, load average: 0.24, 0.16, 0.15
Tasks: 210 total, 2 running, 198 sleeping, 10 stopped, 0 zombie
%Cpu(s): 2.2 us, 0.2 sy, 4.7 ni, 92.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.1 st
KiB Mem: 16433064 total, 1353396 used, 15079668 free, 180944 buffers
KiB Swap: 0 total, 0 used, 0 free. 700468 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24293 ubuntu 30 10 32828 2576 1608 S 19.3 0.0 0:25.30 fiberlamp
2173 ubuntu 20 0 51200 16496 4952 S 9.3 0.1 263:34.18 Xvnc4
12648 ubuntu 20 0 23668 1732 1180 R 0.3 0.0 0:04.25 top.....
........
grep for '%Cpu(s)'
top -bn1 | grep '%Cpu(s)' | awk -F',' '{printf "CPU id %: %.2f%\n", $4}'
On RedHat Linux 6.2 I'm running free -m and it shows nearly all 8GB used
total used free shared buffers cached
Mem: 7989 7734 254 0 28 7128
-/+ buffers/cache: 578 7411
Swap: 4150 0 4150
But at the same time in top -M I cannot see any processes using all this memory:
top - 16:03:34 up 4:10, 2 users, load average: 0.08, 0.04, 0.01
Tasks: 169 total, 1 running, 163 sleeping, 5 stopped, 0 zombie
Cpu(s): 0.7%us, 0.3%sy, 0.0%ni, 98.6%id, 0.4%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 7989.539M total, 7721.570M used, 267.969M free, 28.633M buffers
Swap: 4150.992M total, 0.000k used, 4150.992M free, 7115.312M cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1863 sroot 20 0 398m 24m 9.8m S 0.3 0.3 3:12.87 App1
1 sroot 20 0 2864 1392 1180 S 0.0 0.0 0:00.91 init
2 sroot 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 sroot RT 0 0 0 0 S 0.0 0.0 0:00.07 migration/0
4 sroot 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
5 sroot RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
6 sroot RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
7 sroot RT 0 0 0 0 S 0.0 0.0 0:00.08 migration/1
8 sroot RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/1
I also tried this ps mem script but it onlt shows about 400MB memory being used.
Don't look at the "Mem" line, look at the one below it.
The Linux kernel consumes as much memory as it can to provide the I/O cache (and other non-critical buffers, but the cache is going to be most of this usage). This memory is relinquished to processes when they request it. The "-/+ buffers/cache" line is showing you the adjusted values after the I/O cache is accounted for, that is, the amount of memory used by processes and the amount available to processes (in this case, 578MB used and 7411MB free).
The difference of used memory between the "Mem" and "-/+ buffers/cache" line shows you how much is in use by the kernel for the purposes of caching: 7734MB - 578MB = 7156MB in the I/O cache. If processes need this memory, the kernel will simply shrink the size of the I/O cache.
Also, as the first line shows
total used free shared buffers cached
Mem: 7989 7734 254 0 28 7128
-/+ buffers/cache: 578 7411
If we add (cached[7128] + buffers[28] + free[254]), we will get approximately the second line's free[7411] value
7128 + 28 + 254 = 7410
If the cached is small, try this command:
ps aux --sort -rss