I know the argument "-C" which can filter on the command.

But in my scenario I may have some processes with the same process name and different args.

How to filter on these args?

(I know I could pipe to grep later, but that is not 100% accurate. I.e.: when an arg may contain another arg, such as if I want to filter by the arg "bar" in these list:

| Process | Command | Arguments |
| 23      | foo     | bar       |
| 48      | foo     | barbaz    |

PS: I'm guessing most answers are going to recommend me to use "pgrep" instead of "ps", but I would like to avoid this if possible.


I don't think you can achieve that using ps alone. You're right that filtering using grep is not ideal, but if you're willing to give pgrep a chance then you can come quite close to your requirement with:

ps -p $(pgrep -f bar -d',')

The -f option makes pgrep search the full command line (instead of only the process name), while the -d option specifies the delimiter to use to string the pids together.

Passing the pid list to ps thus limits the output to those that contain the keyword in the full command (which includes the arguments).

In your example where you want to match "foo bar" but not "foo barbaz", you search for an exact match with:

ps -p $(pgrep -f "foo bar$" -d',')   # Note the eol anchor '$'
  • why are you proposing pgrep and ps in the same line? cannot pgrep act as standalone fine? – knocte Jan 9 '13 at 17:13
  • You mentioned you were keen on using ps and not pgrep so I was working on the assumption that you need some options that pgrep cannot offer. Hence my example which uses pgrep only for narrowing down the PIDs for ps. If you simply need the PIDs, ignore the ps bit. – Shawn Chin Jan 9 '13 at 17:21
  • right ok, I might give pgrep a go, but how do you specify an EOL as a value for the -d flag? – knocte Jan 9 '13 at 17:26

I do not believe that such an option exists - at least it does not exist in my version of ps. I doubt that many ps implementations out there have such an option - the command name is pretty straight-forward to define, which is definitely not the case when it comes down to command arguments.

In any case, the Unix philosophy is to chain many simple tools (e.g. ps and grep) to perform more complex tasks. One could argue that even ps -C is pushing it - in fact I believe that the MacOS X ps does not have such an option.

What exactly are you trying to do? What is the context to your question? Because, from my experience, in 99% of the cases where people are automating stuff by parsing the output of ps they are going about whatever they are doing the wrong way...

  • I need to extract the PIDs of an app at pre-deployment time, to later kill it at post-deployment (I do this because if wanted to filter when doing the Kill operation, I'm guessing it would be even harder because I would need to do it ordering processes by creation time). And I need to filter by args because if I don't, I may be killing other application that is running in the same server, under the same user, but with different args. – knocte Jan 9 '13 at 17:17
  • and I already explained why I don't like the grep option – knocte Jan 9 '13 at 17:19
  • @knocte: the standard way to do this on Unix-like systems is to have the launcher script store the server PID in a file after the process is launched. Any other solution is prone to failure... – thkala Jan 9 '13 at 17:19
  • ...some programs also offer the option to create the PID file themselves, which usually includes handling its removal on termination. – thkala Jan 9 '13 at 17:21
  • 1
    @knocte: well, in a bash launcher script you would use the $? and $! special parameters. How are you launching your processes? – thkala Jan 9 '13 at 18:03

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.