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 ?


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.


I normally use this commend

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.