8

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 4

9

Try this:

ps o state,command axh | grep "^[SD]" | cut -b 3-

for listing commands of processes with an interruptable and uninterruptable sleep state.

  • ps outputting only state and commands of all processes (ax) and h removes the header line.
  • grep filters processes other than the two sleep states
  • cut is used to remove the state output again.
  • Optionally replace command with ucmd if 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.

1
  • @Thor Thanks for you now removed comment. I've updated my answer.
    – gertvdijk
    Sep 15, 2012 at 15:30
1

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 }'
  • -n1 top runs only once and exits.
  • NR > 7 skips header.
  • $8 ~ /S|D/ selects programs which are in state D or S.

Possible states are, from top(1):

      'D' = uninterruptible sleep
      'R' = running
      'S' = sleeping
      'T' = traced or stopped
      'Z' = zombie
2
  • 1
    It chops off the first character of the process name; you need to put cut -c40-41,62- instead of cut -c40-41,63-
    – user76204
    Sep 15, 2012 at 16:49
  • It is actually 63 here so using specific column numbers is unreliable. I switched the parsing over to awk.
    – Thor
    Sep 15, 2012 at 17:12
1
ps -e S 

will show you sleeping processes.

3
  • Uhm... no. It will show all processes by providing the -e option.
    – gertvdijk
    Mar 31, 2014 at 12:48
  • ps -e S does in fact give you all the processes that are in a Sleep state. Very helpful.
    – rob
    Apr 17, 2015 at 18:00
  • 1
    @rob of course it does, because it shows all processes ... including those not in a sleep state, which is what -e does. S is: "Sum up some information, such as CPU usage, from dead child processes into their parent." Nothing in this command line will filter out non-sleeping processes.
    – muru
    Oct 26, 2017 at 4:12
0

Using 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .