Display processes using the most CPU or memory using nothing more than basic Linux utilities.

Display processes using the most CPU

Display the top ten processes using the most CPU, including additional information like the exact date when the process was started and how much CPU time it has already used.

$ ps -ax -opid,lstart,pcpu,cputime,command --sort=-%cpu,-cputime | head -11
PID                  STARTED %CPU     TIME COMMAND
  199 Thu Feb  1 15:44:40 2018  0.6 00:46:59 [md1_raid1]
22111 Tue Feb  6 00:02:53 2018  0.2 00:02:17 /usr/bin/ruby /usr/bin/jekyll serve --future --port 3000 --host 10.66.91.53
24551 Tue Feb  6 00:06:44 2018  0.2 00:01:50 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon -e /var/log/icinga2/error.log
28123 Tue Feb  6 11:42:55 2018  0.2 00:00:21 top
 2113 Thu Feb  1 15:44:46 2018  0.1 00:12:07 [z_wr_iss]
    7 Thu Feb  1 15:44:38 2018  0.1 00:09:04 [rcu_sched]
24620 Tue Feb  6 00:06:45 2018  0.1 00:00:52 postgres: 9.6/main: icinga_ido icinga_ido 10.66.91.37(55272) idle in transaction
 8689 Thu Feb  1 15:45:33 2018  0.0 00:06:04 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon -e /var/log/icinga2/error.log
 8454 Thu Feb  1 15:45:31 2018  0.0 00:05:57 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon -e /var/log/icinga2/error.log
 7306 Thu Feb  1 15:45:19 2018  0.0 00:05:50 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon -d -e /var/log/icinga2/icinga2.err

Processes using the most memory

Display the top ten processes using the most memory, including additional information like the exact date when the process was started and how much CPU time it has already used.

$ ps -ax -opid,lstart,pmem,rss,command --sort=-pmem,-rss | head -11
PID                  STARTED %MEM   RSS COMMAND
 6038 Mon Feb  5 23:06:45 2018  1.0 21464 postgres: 9.6/main: icinga_ido icinga_ido 10.66.91.37(55272) idle in transaction
   94 Thu Feb  1 14:45:05 2018  0.8 18772 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf
  438 Thu Feb  1 14:45:15 2018  0.8 17188 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon -e /var/log/icinga2/error.log
   37 Thu Feb  1 14:44:59 2018  0.7 15936 /lib/systemd/systemd-journald
  104 Thu Feb  1 14:45:09 2018  0.7 15284 postgres: 9.6/main: checkpointer process
  106 Thu Feb  1 14:45:09 2018  0.3  7476 postgres: 9.6/main: wal writer process
  107 Thu Feb  1 14:45:09 2018  0.2  4828 postgres: 9.6/main: autovacuum launcher process
    1 Thu Feb  1 14:44:58 2018  0.2  4788 /sbin/init
  464 Thu Feb  1 14:45:15 2018  0.2  4640 /usr/lib/x86_64-linux-gnu/icinga2/sbin/icinga2 --no-stack-rlimit daemon -e /var/log/icinga2/error.log
  108 Thu Feb  1 14:45:09 2018  0.1  3540 postgres: 9.6/main: stats collector process

Processes using more than a specified percentage of CPU and memory

Display processes using at least 10% of available memory and more than 5% of CPU.

$ ps -ax --no-headers -opid,pmem,pcpu,command | \
  awk '$2&ht;=10 && $4>5 {printf "Process %-6s is using %6s%% mem and %6s%% cpu -- %s\n",$1,$2,$3,substr($0, index($0,$4)) }'
Process 24199  is using    10.1% mem and    8.0% cpu -- php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
Process 24620  is using    20.1% mem and    5.1% cpu -- postgres: 9.6/main: icinga_ido icinga_ido 10.66.91.37(55272) idle in transaction

Display processes using at least 20% of available memory and more than 10% of CPU.

$ ps -ax -opid,pmem,rss,pcpu,cputime,command | \
  awk 'NR==1 { print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t\t" $6 } NR>1 && $2>=20 && $4>10 {print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" substr($0, index($0,$6)) }'
PID	%MEM	RSS	%CPU	TIME		COMMAND
6038	20.0	414640	10.1	00:00:52	postgres: 9.6/main: icinga_ido icinga_ido 10.66.91.37(55272) idle in transaction