Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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 am looking to see if a specific process is running as a specific user. For which I can do this

ps --no-headers -C command -o user | grep user

Which is pretty neat, it however gets slightly ugly if I then want to print the arguments (which may interfer with the grep for the username!)

ps --no-headers -C command -o user,pid,comm | egrep \^user

This would get even messer if I didnt want the user as the first or last column. What would be neater is if I could specify the -C and -U options in PS like this

ps --no-headers -C command -U user -o pid

This however doesnt work as the two clauses act as ORs rather than ANDs. Is there some function of 'ps' that I'm missing, or maybe another neat one liner I could use? Cheers!

share|improve this question
up vote 3 down vote accepted

pgrep supports various conditions:

pgrep -x -u "user" "command"

Replace -x with -f to also match the arguments; add -l to display command line.

You can also read the command line directly from /proc/PID/cmdline (null-separated):

if pids=$(pgrep -x -u "user" "command"); then
    for pid in $pids; do
        tr "\0" " " < /proc/$pid/cmdline; echo
    done
fi
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .