Skip to content

Instantly share code, notes, and snippets.

@hn-support
Last active December 26, 2020 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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.
Debug script to log all cron output to a logfile
#!/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"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment