Cron job stderr to email AND log file?

前端 未结 9 1701
离开以前
离开以前 2021-01-06 07:57

I have a cron job:

$SP_s/StartDailyS1.sh >$LP_s/MirrorLogS1.txt

Where SP_s is the path to the script and LP_s

9条回答
  •  醉话见心
    2021-01-06 08:35

    My experience (ubuntu) is that 'crontab' only emails 'stderr' (I have the output directed to a log file which is then archived). That is useful, but I wanted a confirmation that the script ran (even when no errors to 'stderr'), and some details about how long it took, which I find is a good way to spot potential trouble.

    I found the way I could most easily wrap my head around this problem was to write the script with some duplicate 'echo's in it. The extensive regular 'echo's end up in the log file. For the important non-error bits I want in my crontab 'MAILTO' email, I used an 'echo' that is directed to stderr with '1>&2'.

    Thus this:

        Frmt_s="+>>%y%m%d %H%M%S($HOSTNAME): " # =Format of timestamp: "(): "
        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, the lines beginning 'ssh:' and 'rsync:' do not appear):

    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 doesn't satisfy my initial desire to "send both stdout AND stderr to the logfile" (stderr, and only the 'echo'ed lines with '1>&2' go to the email; stdout goes to the log), but I find this is better than my initially imagined solution, as the email finds me and I don't have to go looking for problems in the log file.

提交回复
热议问题