diff --git a/Utility/IsInteractive.sh b/Utility/IsInteractive.sh new file mode 100644 index 0000000..40390af --- /dev/null +++ b/Utility/IsInteractive.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +is_interactive() { + case $- in + *i*) return 1 ;; # Interaktive Shell + *) return 0 ;; # Nicht-interaktive Shell + esac +} diff --git a/Utility/Log.sh b/Utility/Log.sh index c0f25e6..11a8f6d 100755 --- a/Utility/Log.sh +++ b/Utility/Log.sh @@ -1,5 +1,9 @@ #!/bin/bash +# === IMPORTS === +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/IsInteractive.sh" + # === Variablen === LOGDIR="/tmp/skript-logs" LOGFILE="$LOGDIR/$(basename "$0" .sh)_$(date +'%Y-%m-%d').log" @@ -13,5 +17,11 @@ log() { touch "$LOGFILE" fi - echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOGFILE" >/dev/tty + if is_interactive; then + # Interaktive Shell + echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOGFILE" >/dev/tty + else + # Crontab oder nicht-interaktive Umgebung + echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >>"$LOGFILE" + fi }