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 have cobbled together a command to return the process ID of a running daemon:

ps aux | grep daemon_name | awk "{ print \$2 }"

It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?

I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.

share|improve this question
add comment

4 Answers

up vote 16 down vote accepted

grep's -v switch reverses the result, excluding it from the queue. So make it like:

ps aux | grep daemon_name | grep -v grep | awk "{ print \$2 }"

Upd. You can also use -C switch to specify command name like so:

ps -C daemon_name -o pid=

The latter -o determines which columns of the information you want in the listing. pid lists only the process id column. And the equal sign = after pid means there will be no column title for that one, so you get only the clear numbers - PID's.

Hope this helps.

share|improve this answer
Perfect, thank you! –  tak Apr 7 '12 at 12:24
add comment

Avoid parsing ps's output if there are more reliable alternatives.

pgrep daemon_name
pidof daemon_name
share|improve this answer
add comment

You can use a character class trick. "[d]" does not match "[d]" only "d".

 ps aux | grep [d]aemon_name | awk "{ print \$2 }"

I prefer this to using | grep -v grep.

share|improve this answer
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code. –  tak Apr 7 '12 at 12:29
I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"? –  Damian Nowak Sep 28 '13 at 21:13
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear. –  RedGrittyBrick Sep 28 '13 at 21:34
Okay, I was wrong about ARGV. This is just a regexp. Thanks. –  Damian Nowak Sep 29 '13 at 1:29
Devilishly clever! Had to think about it for a moment -- thanks! –  Subfuzion Oct 23 '13 at 18:46
add comment

The ps -C option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:

ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'

No need to escape anything in single quotation marks. ps aux will give you the full list of processes on most Unix based systems and awk is typically installed by default.

share|improve this answer
add comment

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.