4

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?

trox
  • 306
  • 4
  • 12
  • Would `pgrep ssh-agent` be sufficient or do you really need the information that `ps` will give you? – timss May 09 '13 at 23:01
  • Or even `ps aux | grep -f <(pgrep ssh-agent)` (: – Rubens May 09 '13 at 23:03
  • I only need the PID and process name, that's it. However, Im running this script on various unix/linux. I just tested pgrep on AIX and of course, it's not installed. I would prefer it to be as generic as possible, using pure grep only. – trox May 09 '13 at 23:06
  • You might consider using `grep [s]sh-agent` or similar to prevent the self-match, hence avoiding the `grep -v grep`. – martin clayton May 09 '13 at 23:10
  • Also, look into the `-o` option of `ps`, you can use it to format the output, so you just get what you need. – martin clayton May 09 '13 at 23:13
  • Thx Martin for the good advice, I will most certainly learn from it. – trox May 09 '13 at 23:22

3 Answers3

3

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!

Community
  • 1
  • 1
trox
  • 306
  • 4
  • 12
2

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

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
  • Thank you very much! $NF what was I looking for. about grep -v grep, I knew that, however it seemed kind of awkward that you could'nt actualy delimit properly the ps command... Just saying. – trox May 09 '13 at 23:10
  • ps aux | grep ssh-agent | awk '{ print $1 " " $NF }' foo ssh-agent foo ssh-agent and should return: foo ssh-agent foo grep ssh-agent – trox May 09 '13 at 23:13
  • woopp hehe replace $1 with $2 .. or whatever or don't use it.. that was just an example – Ahmed Masud May 09 '13 at 23:14
  • Sure, thank you, your answer helped a lot. I'm good with it, and guess it's gonna work on any unix/linux. $NF should do the trick. Too bad I did not know about it :) – trox May 09 '13 at 23:17
  • 1
    ;-) i used to teach shell programming, and I developed a course for it a long time ago (more than 15 years ago) I think some of what I used to teach actually stuck back to me hehehe – Ahmed Masud May 09 '13 at 23:22
1

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.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • using /proc filesystem is generally an incompatible way to script. what-if he wanted to move this script to a BSD or a solaris machine or something else? You are essentially doing in your for loop what ps does on Linux (it reads /proc) and probably doing a bad job of it because ps will actually do other tests e.g. faccess so you won't get Permission Denied ugliness for processes you don't have access to – Ahmed Masud May 09 '13 at 23:13
  • As I said, what suits him better depends on his (unmentioned) circumstances. To depend on the fact that this specific ps implementation prints the command name in the 12th column might also be assuming too much. – Alfe May 09 '13 at 23:16
  • Yes thank you very much Alfe, I will also try your method. I guess the /proc is not for me because as stated, I want my script to basicaly run on anything seemlessly. – trox May 09 '13 at 23:19
  • Tricky task, that. Really: real portable code isn't written in shell ;-) – Alfe May 09 '13 at 23:20
  • Well I guess I will do my best :) Portability is very important for me. In a personal opinion, a code shouldn't be rewritten/adapted. Sorry I come from the Java world ^^ – trox May 09 '13 at 23:24
  • How about using C or Python for this? ;-) – Alfe May 09 '13 at 23:26
  • 1
    Why not Python indeed. I'll look into it. Again thank you so much for the all the kind help! Stackoverflow community rox! – trox May 09 '13 at 23:30