How to find sleeping process in Ubuntu?
By Emma Johnson •
Is there any way to find sleeping processes in Ubuntu?
I can see top can list out number of sleeping processes, but I want them to be listed with their name.
Are there any commands for that?
4 Answers
Try this:
ps o state,command axh | grep "^[SD]" | cut -b 3-for listing commands of processes with an interruptable and uninterruptable sleep state.
psoutputting only state and commands of all processes (ax) andhremoves the header line.grepfilters processes other than the two sleep statescutis used to remove the state output again.- Optionally replace
commandwithucmdif you don't need the full name including all arguments.
This is probably suboptimal scripting here, but I couldn't find a quick way to have ps filtered for a specific state.
You could grab the information from top, which can be run in batch mode (-b).
top -bn1 | awk 'NR > 7 && $8 ~ /S|D/ { print $12 }'-n1top runs only once and exits.NR > 7skips header.$8 ~ /S|D/selects programs which are in stateDorS.
Possible states are, from top(1):
'D' = uninterruptible sleep 'R' = running 'S' = sleeping 'T' = traced or stopped 'Z' = zombie2
ps -e S will show you sleeping processes.
3Using awk and ps
ps o pid,state,command | \ awk ' {if ($2 == "T") {printf "%s\t%s\t%s\n","Sleeping",$1,$3};}'Example
Start mc in a terminal, open a new terminal and start the commands below
kill -STOP $(pgrep mc)
ps o pid,state,command | \ awk ' {if ($2 == "T") {printf "%s\t%s\t%s\n","Sleeping",$1,$3};}'Output is
Sleeping 21668 mc