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

If you just want the pid of the process you can make use of pgrep if available. pgrep <command> will return the pid of the command (or list of pids in case there are more than one instance of the command running, in which case you can make use of head or other appropriate commands)
Hope this helps!


Just one more command needed; you want only the first field from a line of space-separated values:

ps -A -o pid,cmd|grep xxx | grep -v grep |head -n 1 | awk '{print $1}'

Well, two. I added another grep to remove grep itself from the output.


Just use pgrep, it's much more straight forward

pgrep -o -x xxxx

The above selects the oldest process with the exact name