My experience (ubuntu) is that "crontab" only sends "stderr" messages (I have an output directed to a log file, which is then archived). This is useful, but I wanted to get confirmation that the script worked (even if there were no errors in "stderr") and some details about how much time had passed, which I consider to be a good way to detect a potential problem.
I found a way that I could most easily wrap my head around this problem, was to write a script with some duplicate echo in it. Extensive regular echo ends in a log file. For the important error bits that I want in the crontab email "MAILTO", I used an "echo" which is directed to stderr using "1> & 2".
In this way:
Frmt_s="+>>%y%m%d %H%M%S($HOSTNAME): " # =Format of timestamp: "<YYMMDD HHMMSS>(<machine name>): " echo `date "$Frmt_s"`"'$0' started." # '$0' is path & filename echo `date "$Frmt_s"`"'$0' started." 1>&2 # message to stderr # REPORT: echo "" echo "================================================" echo "================================================" 1>&2 # message to stderr TotalMins_i=$(( TotalSecs_i / 60 )) # calculate elapsed mins RemainderSecs_i=$(( TotalSecs_i-(TotalMins_i*60) )) Title_s="TOTAL run time" Summary_s=$Summary_s$'\n'$(printf "%-20s%3s:%02d" "$Title_s" $TotalMins_i $RemainderSecs_i) echo "$Summary_s" echo "$Summary_s" 1>&2 # message to stderr echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 1>&2 # message to stderr echo "" echo `date "$Frmt_s"`"TotalSecs_i: $TotalSecs_i" echo `date "$Frmt_s"`"'$0' concluded." # '$0' is path & filename echo `date "$Frmt_s"`"'$0' concluded." 1>&2 # message to stderr
Sends me an email containing this (when there are no errors, lines beginning with 'ssh:' and 'rsync:' are not displayed):
170408 030001(sb03): '/mnt/data1/LoSR/backup_losr_to_elwd.sh' started. ssh: connect to host 000.000.000.000 port 0000: Connection timed out rsync: connection unexpectedly closed (0 bytes received so far) [Receiver] rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.0] ssh: connect to host 000.000.000.000 port 0000: Connection timed out rsync: connection unexpectedly closed (0 bytes received so far) [Receiver] rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.0] ================================================ S6 SUMMARY (mins:secs): 'Templates' 2:07 'Clients' 2:08 'Backups' 0:10 'Homes' 0:02 'NetAppsS6' 10:19 'TemplatesNew' 0:01 'S6Www' 0:02 'Aabak' 4:44 'Aaldf' 0:01 'ateam.ldf' 0:01 'Aa50Ini' 0:02 'Aadmin50Ini' 0:01 'GenerateTemplates' 0:01 'BackupScripts' 0:01 TOTAL run time 19:40 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 170408 031941(sb03): '/mnt/data1/LoSR/backup_losr_to_elwd.sh' concluded.
This does not satisfy my original desire to "send both stdout AND stderr to the log file" (stderr, and only the "echo lines" with "1> and 2" go through email; stdout goes to log), but I believe that this better than my originally intended solution, as the email finds me and I don't need to look for problems in the log file.
Steph source share