Search code examples
bashgenericsawkps

Bash generic "ps aux" process by strict name


I'm trying to have a ps aux command listing only real ssh-agent process. I've noticed that on some distros, I have unwanted process showing up, like command colors and such. I need to do this because I need to ensure the real ssh-agent process is running in a script (don't bother, I already have a loop for it...).

So I figured out I need to use something like that in my test routine:

#!/bin/bash
ps aux | grep ssh-agent | grep -v grep | awk '{print $12}'

My question is: Would the awk $12 work on any unix/linux env using bash with any bash versions?

Also, if I remove "grep -v grep" and do this:

ps aux | grep ssh-agent | awk '{print $12}'

output is:

ssh-agent
ssh-agent

Now:

ps aux | grep ssh-agent

output is:

foo 18153478  0.0  0.0  536  844      - A      Apr 25  0:00 ssh-agent
foo 31260886  0.0  0.0  252  264  pts/0 A    22:38:41  0:00 grep ssh-agent

That means, the space between "grep ssh-agent" is interpreted as a delimiter by the awk command. Aynthing possible to overcome that? I already tried using tab delimiter but that's not it. It seems the delimiter of the command are simple "space" characters. Is it possible to enforce tab delimiters for "ps" command output?

Any idea?


Solution

  • OK I guess I found it, really easy and straightforward:

    ps -e -o pid,comm | grep ssh-agent
    

    Works just fine.

    Answer found here: https://unix.stackexchange.com/questions/22892/how-do-use-awk-along-with-a-command-to-show-the-process-id-with-the-ps-command/22895#22895

    And adapted with a | grep ssh-agent

    Also suggested by Martin. Thank you all for sharing your experience!