Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 19 Jul 2001 20:39:56 -0500
From:      Mike Meyer <mwm@mired.org>
To:        Alex Dyas <ADyas@twowaytv.com>, Olivier Cortes <olive@deep-ocean.net>
Cc:        questions@freebsd.org
Subject:   RE: Shell scripting gurus I nedd your help
Message-ID:  <15191.35820.325672.101326@guru.mired.org>
In-Reply-To: <55717934@toto.iv>

next in thread | previous in thread | raw e-mail | index | archive | help
Alex Dyas <ADyas@twowaytv.com> types:
> $ for address in `cat admins.txt`;do ls -al | mail -s "Ouput of ls command"
> $address;done

You're running the ls once per address. This may not give the desired
results, and certainly uses more CPU than generating the list once.

Olivier Cortes <olive@deep-ocean.net> types:
> -----------------------------------
> #!/bin/blash
> 
> COMMAND='ls -la'
> MSGFILE="my_temp_fic_1012380293476234"
> ADDRFILE="admins.txt"
> 
> $COMMAND > $MSGFILE 2>&1 
> 
> while read ADDR
> do
> mail -s "$COMMAND" $ADDR < $MSGFILE
> done < $ADDRFILE

Both of these work, but have the same basic problem - they create a
mail process for every address, which uses a lot of resources. It also
creates a message - with a different message id - for every user,
which can screw up message threading.  It also means you have to wait
for the mail to be delivered before starting the next command, which
can add up to quite a bit of real time. You could background the mail
commands, but that risks running out of processes, or spool space, or
some other resource.

If you hand the message and addresses to the MTA properly, you should
only have to wait for one message to be delivered. The MTA will then
give it a messageid, and deliver it as efficiently as possible. In the
extreme case, the MTA might deliver one message along with the
destination addresses to a remote server, which would just keep one
copy of the message along with pointers in each readers mailbox,
meaning you've just multiplied cpu, network and disk usage by the
number of addresses.

Since the mail command can handle multiple addresses, sending the
message to `cat admins.txt` is one way of handing things off to the
MTA. Going with an alias gives the MTA an even better chance to lower
usage, and makes the list available for other uses as well.

	<mike
--
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15191.35820.325672.101326>