Skip to content

Instantly share code, notes, and snippets.

@hn-support
Last active December 26, 2020 18:19
Show Gist options
  • Save hn-support/c8842fd4a904484e800d8f4f486365da to your computer and use it in GitHub Desktop.
Save hn-support/c8842fd4a904484e800d8f4f486365da to your computer and use it in GitHub Desktop.

Revisions

  1. hn-support revised this gist May 3, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions cron-debugger.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,11 @@
    #!/bin/bash
    # This script is a debug utility for cronjobs as explained in:
    # - https://support.hypernode.com/knowledgebase/configure-cronjobs-on-hypernode/
    # It logs all output and timing to a log file
    #
    # To use it, download the script, add the executable bit and put it in your cronjob:
    # */5 * * * * /data/web/bin/debug-cron php -f /data/web/public/cron.php

    LOGDIR="/data/web/public/var/log/crons"
    TIMESTAMP="$( date '+%Y%m%d%H%M' )"
    FILENAME="$LOGDIR/cronjob-${TIMESTAMP}.log"
  2. hn-support revised this gist Sep 22, 2016. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions cron-debugger.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@ LOGDIR="/data/web/public/var/log/crons"
    TIMESTAMP="$( date '+%Y%m%d%H%M' )"
    FILENAME="$LOGDIR/cronjob-${TIMESTAMP}.log"


    function log() {
    while read LOG ; do
    echo "$(date) - $LOG" | tee -a $FILENAME
    @@ -27,4 +26,4 @@ fi
    echo "Running cron job \'${COMMAND}\'" | log
    ${COMMAND} | log
    EXITCODE="$?"
    echo "Cron job \'${COMMAND}\' finished with exit code ${EXITCODE}" | log
    echo "Cron job \'${COMMAND}\' finished with exit code ${EXITCODE}" | log
  3. hn-support created this gist Sep 22, 2016.
    30 changes: 30 additions & 0 deletions cron-debugger.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/bin/bash
    LOGDIR="/data/web/public/var/log/crons"
    TIMESTAMP="$( date '+%Y%m%d%H%M' )"
    FILENAME="$LOGDIR/cronjob-${TIMESTAMP}.log"


    function log() {
    while read LOG ; do
    echo "$(date) - $LOG" | tee -a $FILENAME
    done < "${1:-/dev/stdin}"
    }

    ## Test if log dir is present
    [ -d "${LOGDIR}" ] || mkdir -p "${LOGDIR}"

    ## Get arguments from script
    SCRIPTNAME="$0"
    COMMAND="${@}"

    ## Exit if no arguments
    if [ "${COMMAND}x" == "x" ] ; then
    echo "$0 Usage $SCRIPTNAME <command>"
    exit 1
    fi

    ## Log and run
    echo "Running cron job \'${COMMAND}\'" | log
    ${COMMAND} | log
    EXITCODE="$?"
    echo "Cron job \'${COMMAND}\' finished with exit code ${EXITCODE}" | log