I want to do something like this: 30 2 * * * MAILTO=testaddr at 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? -- *** E-Mail is NOT a SECURE channel *** James B. Byrne mailto:ByrneJB at Harte-Lyne.ca Harte & Lyne Limited http://www.harte-lyne.ca 9 Brockley Drive vox: +1 905 561 1241 Hamilton, Ontario fax: +1 905 561 0757 Canada L8E 3C3
On 07/13/2011 12:37 PM, James B. Byrne wrote:> I want to do something like this: > > 30 2 * * * MAILTO=testaddr at 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 at 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? -- *** E-Mail is NOT a SECURE channel *** James B. Byrne mailto:ByrneJB at Harte-Lyne.ca Harte & Lyne Limited http://www.harte-lyne.ca 9 Brockley Drive vox: +1 905 561 1241 Hamilton, Ontario fax: +1 905 561 0757 Canada L8E 3C3
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. -- *** E-Mail is NOT a SECURE channel *** James B. Byrne mailto:ByrneJB at Harte-Lyne.ca Harte & Lyne Limited http://www.harte-lyne.ca 9 Brockley Drive vox: +1 905 561 1241 Hamilton, Ontario fax: +1 905 561 0757 Canada L8E 3C3
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 at 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