How to sort ps output to find processes realtime priority and identify processess currently occupied running queue

cpupriorityprocesspsreal time

How to properly identify real-time processes currently occupied CPU queue and count them using ps? I know there is a bunch of fileds like prio,rtprio,pri,nice but do not know correct to use. It seems I need use something like ps -eo rtprio,prio,cpu,cmd --sort=+rtprio to get full list, but it do not seems for me right since a lot of processes got with - sign at RTPRIO column. For example, I have 48 cores system running Oracle Linux and try to identify following questions:

  1. What processes occupied run queue? What's a count of them?
  2. How to identify processes that run in Real Time mode or with increased priority?

Best Answer

A list of non-zero CPU % processes:

ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm --sort=+pcpu | awk '$8!=0.0 {print}' | awk 'NR>1'

To count them

ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm --sort=+pcpu | awk '$8!=0.0 {print}' | awk 'NR>1' | wc -l

To see this continuously updated, but them in a file called processes.sh:

#!/bin/bash
ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm --sort=+pcpu | awk '$8!=0.0 {print}' | awk 'NR>1'

and make it executable with chmod +x processes.sh. Now run it with watch for live updating:

watch ./processes.sh
Related Question