Skip to content

Instantly share code, notes, and snippets.

@shortcord
Created February 28, 2022 17:20
Show Gist options
  • Save shortcord/d336f05e1dbbd5c1291497aa061aa2db to your computer and use it in GitHub Desktop.
Save shortcord/d336f05e1dbbd5c1291497aa061aa2db to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#shellcheck disable=2155
# Cronic v3 Edited - cron job report wrapper
# Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever.
# Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/\
# Source: https://habilis.net/cronic/
function exitTrap {
## Short out if $TMP is root for some reason
if [[ "${TMP}" == "/" ]]; then
echo "\$TMP is /, not touching that!";
exit 1;
fi;
## Only cleanup if $TMP is defined
if [[ -n "${TMP}" ]]; then
rm -rf "${TMP}";
fi;
}
trap exitTrap EXIT;
set -eu;
readonly TMP=$(mktemp -d);
readonly OUT=$TMP/cronic.out;
readonly TRACE=$TMP/cronic.trace;
readonly PATTERN="^${PS4:0:1}\\+${PS4:1}";
ERR=$TMP/cronic.err;
set +e;
"$@" > "${OUT}" 2> "${TRACE}";
RESULT=$?;
set -e;
## Check for error, if any inverse match and pipe into ERR file
if grep -aq "${PATTERN}" "${TRACE}"; then
grep -av "${PATTERN}" "${TRACE}" > "${ERR}";
else
## Else set cat TRACE into ERR
cat "${TRACE}" > "${ERR}";
fi;
## If RESULT is not equal to 0 OR $ERR exists and size is > 0 bytes
if [[ "${RESULT}" -ne 0 ]] || [[ -s "${ERR}" ]]; then
echo "Cronic detected failure or error output for the command:";
echo "$@";
echo "";
echo "RESULT CODE: ${RESULT}";
echo "";
if [[ -s "${ERR}" ]]; then
echo "ERROR OUTPUT:";
cat "${ERR}";
echo "";
fi;
echo "STANDARD OUTPUT:";
cat "${OUT}";
echo "";
if [[ -s "${TRACE}" ]]; then
echo "TRACE Output:";
cat "${TRACE}";
fi;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment