How to get the pid of a running process using a single command that parse the output of ps?

Posted by Sorin Sbarnea on Super User See other posts from Super User or by Sorin Sbarnea
Published on 2012-05-23T17:25:38Z Indexed on 2012/09/30 9:40 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

I am looking for a single line that does return the pid of a running process.

Currently I have:

ps -A -o pid,cmd|grep xxx|head -n 1

And this returns the fist pid, command. I need only the first number from the output and ignore the rest. I suppose sed or awk would help here but my experience with them is limited.

Also, this has another problem, it will return the pid of grep if the xxx is not running.

It's really important to have a single line, as I want to reuse the output for doing something else, like killing that process.

© Super User or respective owner

Related posts about bash

Related posts about sed