Skip to content

Instantly share code, notes, and snippets.

@ashishb888
Last active October 5, 2020 10:30
Show Gist options
  • Save ashishb888/e583ba9b75dc73301be5e8ba854fed39 to your computer and use it in GitHub Desktop.
Save ashishb888/e583ba9b75dc73301be5e8ba854fed39 to your computer and use it in GitHub Desktop.
Linux top command's first ten records by CPU usage in batch mode
#!/bin/bash
timestamp=`date +%FT%H-%M-%S`
sleepMillis=10
while true
do
top -b -o +%CPU | head -n 17 >> /var/tmp/cpu-stats-$timestamp.log
sleep $sleepMillis
done
## Sample output
# top - 19:00:27 up 15 days, 15:42, 1 user, load average: 9.07, 8.21, 5.87
# Tasks: 722 total, 1 running, 721 sleeping, 0 stopped, 0 zombie
# %Cpu(s): 16.5 us, 1.6 sy, 0.0 ni, 81.7 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
# KiB Mem : 52751721+total, 20111702+free, 10653513+used, 21986505+buff/cache
# KiB Swap: 8388604 total, 8388604 free, 0 used. 41944873+avail Mem
#
# PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
# 165177 myuser 20 0 97.5g 45.7g 13760 S 418.8 9.1 60:05.68 java
# 167286 myuser 20 0 69.0g 43.6g 14004 S 268.8 8.7 110:49.24 java
# 167496 myuser 20 0 28.8g 4.7g 13936 S 187.5 0.9 13:26.77 java
# 168558 myuser 20 0 37.9g 2.8g 14548 S 187.5 0.6 29:50.05 java
# 111 root 20 0 0 0 0 S 6.2 0.0 23:59.92 ksoftirqd+
# 174609 myuser 20 0 162552 2748 1528 R 6.2 0.0 0:00.02 top
# 1 root 20 0 192452 5452 2636 S 0.0 0.0 6:39.84 systemd
# 2 root 20 0 0 0 0 S 0.0 0.0 0:00.84 kthreadd
# 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0+
# 6 root 20 0 0 0 0 S 0.0 0.0 0:31.01 ksoftirqd+
# top - 18:16:58 up 15 days, 14:58, 1 user, load average: 0.00, 0.01, 0.05
# Tasks: 716 total, 1 running, 715 sleeping, 0 stopped, 0 zombie
# %Cpu(s): 0.1 us, 0.3 sy, 0.0 ni, 99.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
# KiB Mem : 52751721+total, 30282704+free, 4831064 used, 21985910+buff/cache
# KiB Swap: 8388604 total, 8388604 free, 0 used. 52115280+avail Mem
#
# PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
# 164835 myuser 20 0 162552 2740 1528 R 16.7 0.0 0:00.06 top
# 1 root 20 0 192452 5452 2636 S 0.0 0.0 6:39.11 systemd
# 2 root 20 0 0 0 0 S 0.0 0.0 0:00.84 kthreadd
# 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0+
# 6 root 20 0 0 0 0 S 0.0 0.0 0:30.93 ksoftirqd+
# 7 root rt 0 0 0 0 S 0.0 0.0 0:01.03 migration+
# 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
# 9 root 20 0 0 0 0 S 0.0 0.0 16:10.37 rcu_sched
# 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-d+
# 11 root rt 0 0 0 0 S 0.0 0.0 0:04.16 watchdog/0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment