Question

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?

Was it helpful?

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!

OTHER TIPS

First $12 is tied to how many fields ps outputs. It has nothing to with bash.

grep -v grep is a good way to remove the grep process so leave that in;

Now you are not certain if the last field is field 12 or what, I'm not quite certain

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

assuming you are looking to only see ssh-agent (and it has no command-line parameters)

Here is something quick and dirty that will spit out the pid and the complete command-line

ps aux | grep ssh-agent | grep -v grep | awk '{ print $1 " "; for (k = 12; k < NF; k++ ) { printf "%s", k; } printf "\n"; }' 

NF is the number of fields in awk so $NF is the last field.

see if this works for you. There are much cleaner ways of doing this though

I propose to grep for sth like '[s]sh-agent'. This way you prevent getting the grep itself.

I also propose to not use awk (to print the 12th column) but to use cut to print a specific character range of the lines, e. g.:

ps aux | grep '[s]sh-agent' | cut -c 66-

This is depending on the output format of ps of course.

You could have a look at the /proc/ file system instead:

(
  cd /proc
  for p in [0-9]*
  do
    [ "$(readlink $p/exe)" = "/bin/bash" ] &&
      echo "$p $(cat $p/cmdline | tr '\0' ' ')"
  done
)

(Use sth more appropriate instead of /bin/bash of course.)

This on the other hand will depend on the existence of the /proc/ file system. Decide for yourself which is better for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top