5

I am trying to get all the processes for which the value corresponding to the STAT column is X.

I have done this using awk ps -aux | awk {'if ($8 == "S") print $8" "$11'}

However, I would like to do it without using a program other than ps. Is there a way?

2 Answers 2

7

ps has limited filtering capabilities, but even Linux's ps with its myriad options can't filter by status. So you will need an external filtering tool.

You can simplify the set of options. -ax is equivalent to the portable -e to display all processes. You can use -o instead of counting and selecting columns with awk.

ps -e -o stat,command | grep '^S '
2

No, there probably isn't a way. ps has a few process selection options, but (at least the one on my system) doesn't have a way to filter on the status. You can check the manual page (man ps) on your system to confirm.

Note that it's using things like awk, grep, etc. to find the bits of output you're interested in is the normal, expected way to use a Linux/Unix shell. If you do that often, and want to save re-typing it, you can use a shell function defined in your .bashrc or similar, or put a shell script somewhere in $PATH (often, $HOME/bin).

For example, a shell function taking one argument (the status to look for):

ps_for_stat() {
    ps -aux | awk {'if ($8 ==  "'"$1"'") print $8" "$11'}
}

You could then run: ps_for_stat S

2
  • Thanks, I appreciate the extra info on the shell script.
    – edoreld
    Mar 11, 2017 at 10:43
  • Got the shell function working after figuring out that I needed to add an additional line after it: "export -f ps_for_stat". Thanks again!
    – edoreld
    Mar 11, 2017 at 17:31

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.