Ubuntu – Listing PID of sleeping processes

process

I've been reading about processes for a few hours now and I've got a question I'm not able to answer.

How can I display the PID of sleeping processes and also determine and list their number ?

Best Answer

Yes. You can use the ps tool to list all processes and their state:

ps -eo s,pid

If we want to parse this, we need to remove the header:

ps h -eo s,pid

Now we are going to pipe it to an awk command so that we can only print the pids with a state of "S" (sleeping):

ps h -eo s,pid | awk '{ if ($1 == "S" || $1 == "D") { print $2 } }'

And now you have a newline separated list of process IDs that are sleeping.

Do with it what you wish.