1

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!

1 Answer 1

3

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

You must log in to answer this question.

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