I want to do something like this:
30 2 * * * MAILTO=testaddr@harte-lyne.ca; echo "this should be mailed"
I have searched extensively and from what I have read I believe that this should work. But evidently I misapprehend how cron and MAILTO is supposed to work as my example does not cause any mail to be sent as far as I can determine from maillog.
How does one specify unique MAILTOs for different cron jobs where the desired recipient is not the user that runs the cron job?
On 07/13/2011 12:37 PM, James B. Byrne wrote:
I want to do something like this:
30 2 * * * MAILTO=testaddr@harte-lyne.ca; echo "this should be mailed"
I have searched extensively and from what I have read I believe that this should work. But evidently I misapprehend how cron and MAILTO is supposed to work as my example does not cause any mail to be sent as far as I can determine from maillog.
How does one specify unique MAILTOs for different cron jobs where the desired recipient is not the user that runs the cron job?
Like this:
MAILTO=testaddr@harte-lyne.ca 30 2 * * * echo "this should be mailed"
-Mike
On Wed Jul 13 15:03:40 EDT 2011, Michael Best mbest at pendragon.org wrote:
Like this:
MAILTO=testaddr at harte-lyne.ca 30 2 * * * echo "this should be mailed"
That sets MAILTO for the entire crontab does it not? I want to set MAILTO differently for specific crontab entries. Is that possible? How is it done? Or do I have to pipe stuff to /usr/bin/mail explicitly?
James B. Byrne wrote:
On Wed Jul 13 15:03:40 EDT 2011, Michael Best mbest at pendragon.org wrote:
Like this:
MAILTO=testaddr at harte-lyne.ca 30 2 * * * echo "this should be mailed"
That sets MAILTO for the entire crontab does it not? I want to set MAILTO differently for specific crontab entries. Is that possible? How is it done? Or do I have to pipe stuff to /usr/bin/mail explicitly?
30 2 * * * echo "this should be mailed" | mail -s "important email" testaddr@harte-lyne.ca
will work.
mark
centos-bounces@centos.org wrote:
On Wed Jul 13 15:03:40 EDT 2011, Michael Best mbest at pendragon.org wrote:
Like this:
MAILTO=testaddr at harte-lyne.ca 30 2 * * * echo "this should be mailed"
That sets MAILTO for the entire crontab does it not? I want to set MAILTO differently for specific crontab entries. Is that possible? How is it done? Or do I have to pipe stuff to /usr/bin/mail explicitly?
Note: The script that you trigger (a wrapper for the command you really want to run) can have the per-command peculiarities in it. Also: the command in the crontab file can look like 'cmd | mail -s "Subject of mail" user' Also: a file (e.g. 'mumble') in the /etc/cron.d directory is effectively an extension of the /etc/crontab file, and MAILTO can be set there (IIRC it will only effect entries in that /etc/cron.d/mumble file).
Insert spiffy .sig here: Life is complex: it has both real and imaginary parts. Life is not measured by the number of breaths we take, but by the moments that take our breath away.
//me ******************************************************************* This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept for the presence of computer viruses. www.Hubbell.com - Hubbell Incorporated**
On 07/13/2011 01:10 PM, James B. Byrne wrote:
On Wed Jul 13 15:03:40 EDT 2011, Michael Best mbest at pendragon.org wrote:
Like this:
MAILTO=testaddr at harte-lyne.ca 30 2 * * * echo "this should be mailed"
That sets MAILTO for the entire crontab does it not? I want to set MAILTO differently for specific crontab entries. Is that possible? How is it done? Or do I have to pipe stuff to /usr/bin/mail explicitly?
It sets the MAILTO for all entries that follow that MAILTO. You can have multiple MAILTO entries in your crontab, as well as other variables.
If you only have one or two entries that need non-default mail handling, you can put them at the end of the crontab.
-Mike
On Wed, Jul 13, 2011 at 9:10 PM, James B. Byrne byrnejb@harte-lyne.ca wrote:
On Wed Jul 13 15:03:40 EDT 2011, Michael Best mbest at pendragon.org wrote:
Like this:
MAILTO=testaddr at harte-lyne.ca 30 2 * * * echo "this should be mailed"
That sets MAILTO for the entire crontab does it not? I want to set MAILTO differently for specific crontab entries. Is that possible? How is it done? Or do I have to pipe stuff to /usr/bin/mail explicitly?
Easy:
MAILTO="root" 30 2 * * * echo "this should be mailed to root" MAILTO="james@harte.x.x" 30 4 * * * echo "this should be mailed to James" MAILTO="bob" 30 5 * * * echo "this should be mailed to Bob" MAILTO="" 30 6 * * * echo "this should be mailed to no-one"
On Wed Jul 13 15:30:30 EDT 2011, Brunner, Brian T. BBrunner at gai-tronics.com wrote:
Note: The script that you trigger (a wrapper for the command you really want to run) can have the per-command peculiarities in it. Also: the command in the crontab file can look like 'cmd | mail -s "Subject of mail" user' Also: a file (e.g. 'mumble') in the /etc/cron.d directory is effectively an extension of the /etc/crontab file, and MAILTO can be set there (IIRC it will only effect entries in that /etc/cron.d/mumble file).
Thanks. I gather from these and previous comments, and from experimentation, that it is not possible to arbitrarily set the MAILTO variable in the actual crontab entry. One must set it somewhere before that entry and, where necessary, reset it to something else immediately thereafter.
If you want the output of your cronjob to go to a particular place that is a bit too inconvenient to do using the built-in functionality, put a script around your commands and handle the mailing yourself. This is a good general rule whenever something you want to run from cron is not trivial (in the sense of its invocation):
#! /bin/sh
MAILTO=somebody@example.com COMMAND="/usr/local/bin/mycommand" COMMAND_ARGS="some args"
[ -x $COMMAND ] || exit 0
# safe place for temporary files, including cleanup on exit TDIR=`mktemp -qtd mycommand.XXXXXXXXXX` if [ $? -ne 0 ]; then echo "Failed to create temporary directory; aborted" exit 1 fi trap "/bin/rm -rf $TDIR" 0 1 2 15
OUT1=$TDIR/out1 MAIL_OUT=$TDIR/mail-out
# run it $COMMAND $COMMAND_ARGS 2>&1 > $OUT1
# only mail something out if it produced output. Alternately # you might want to check the value of $? if [ -s $OUT1 ]; then
h=`hostname` cat > $MAIL_OUT <<EOF To: $MAILTO Subject: mycommand problem on $h
There is a problem with mycommand on $h. The following is the output of $COMMAND $COMMAND_ARGS
EOF cat $OUT >> $MAIL_OUT /usr/sbin/sendmail -t < $MAIL_OUT fi