Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.