Re: piping unknown number of elements to an array

From: Barry Margolin (barry.margolin_at_level3.com)
Date: 04/29/03


Date: Tue, 29 Apr 2003 17:50:47 GMT

In article <KPadnY5e7dx5PzOjXTWcpw@giganews.com>, <brfg3> wrote:
>I need to write a script that monitors our postfix. When I ps -aux | grep
>"postfix"
>I get a bunch of entries. All I need is the last field, so I changed to ps -aux |
>grep "postfix" | awk '{print $11}' which gives me the last field. The problem is
>that there are many postfix processes, and I really don't know which one is the
>actual postfix daemon and which are forked processes, but I do know that
>if postfix
>is down, nothing shows except the grep statement.
>
>example:
>[root@mailserver root]# ps -aux | grep "postfix" | awk '{print $11}'
>qmgr
>proxymap
>trivial-rewrite
>smtpd
>local
>local
>smtpd
>cleanup
>grep
>
>What I want to do is load each of these lines into an array so I can loop through
>and decide if the array has a running postfix in it or just my grep. How can I do
>that (pipe the output of "ps -aux | grep "postfix" | awk '{print $11}'" into an
>array without knowing how many elements the array will have?

Why do you need an array? Why not just exclude the unwanted ones with an
appropriate regexp?

ps -aux | awk '/postfix/ && !/awk/ {print $11}'

or

ps -aux | awk '/[p]ostfix/ {print $11}'

or if you insist on using grep even though awk has its own regexp matcher:

ps -aux | grep '[p]ostfix' | awk '{print $11}'

or

ps -aux | grep 'postfix' | grep -v grep | awk '{print $11}'

-- 
Barry Margolin, barry.margolin@level3.com
Genuity Managed Services, a Level(3) Company, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


Relevant Pages

  • piping unknown number of elements to an array
    ... I need to write a script that monitors our postfix. ... nothing shows except the grep statement. ... What I want to do is load each of these lines into an array so I can loop through ...
    (comp.unix.shell)
  • Re: postfix at startup
    ... postfix configured satisfactorily. ... How do I get it to start at boot? ... Postfix enabler? ...
    (uk.comp.sys.mac)
  • Re: Mini 10v
    ... All UNIX and Linux boxes should have an MTA. ... Postfix: installed size of 3196. ... really worry about the resources used by a few little postfix processes. ... there is a real value in an MTA that can queue mail and deliver ...
    (Debian-User)
  • Re: postfix help
    ... No, just postfix: ... ps aux | grep sendmail returns nothing ... root is the owner, since I reloaded it as root. ...
    (freebsd-questions)
  • Re: Mini 10v
    ... All UNIX and Linux boxes should have an MTA. ... Postfix: installed size of 3196. ... Though the main memory consumer ... really worry about the resources used by a few little postfix processes. ...
    (Debian-User)