Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need the pid for a process given its owner and its command. I can filter a process per user with "ps -u xxx" and its command by "ps -C yyy", but when I try "ps -u xxx -C yyy", they are combined using OR logic. I need AND logic. How can I accomplish this?

share|improve this question
up vote 8 down vote accepted

Use pgrep?

pgrep -U xxx yyy

it returns just the pid (or pids, if more than one process matches).

share|improve this answer

Use grep?

ps -u xxx | grep yyy | grep -v grep
share|improve this answer

You use comm to find PIDs common to both conditions:

ps -u xxx | sort > /tmp/ps-uxxx
ps -C yyy | sort > /tmp/ps-Cyyy
comm -1 -2 /tmp/ps-uxxx /tmp/ps-Cyyy

Using bash, you can use process substitution to avoid the need for temporary files:

comm -1 -2 <(ps -u xxx | sort) <(ps -C yyy | sort)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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