commit 23c8c9a930a6532b06ce5059e8702389d0b0d19e from: vincent.delft date: Sun Dec 15 07:55:44 2024 UTC add context and improve getopts commit - aef1742de053a863854a0ec6ecdb2e500a045dd9 commit + 23c8c9a930a6532b06ce5059e8702389d0b0d19e blob - 406749d961f6a0b69278f95f23d8b7b3f18921f1 blob + cb42a354aedfbb3ca532f9f7584712ee57da0a5c --- vdcron +++ vdcron @@ -16,13 +16,17 @@ SLEEP=10 TEMP=$(mktemp) FILE= VERSION=0.7.1 -USAGE="$0 +CONTEXT="cli" +USAGE="$0 [context] options are: -h : this help -d : debug -s : force an initial sleep different than the default: $SLEEP seconds -f : use a specific input file - + context: + Context is just an optional name allowing you to to retreive this information in the logs. + This is useful just to know what has been trigered vdcron (cron, apm-resume, ...) + By default the value is \"cmd line\" You are running $0 version $VERSION " PARAMS="hdf:s:" @@ -32,15 +36,33 @@ while getopts "$PARAMS" optchar; do d) DEBUG=1 ;; f) FILE=$OPTARG + if [ ! -f "$FILE" ]; then + echo "Not a valid file: $FILE" + exit 1 + fi ;; s) SLEEP=$OPTARG + if [ ! "$SLEEP" -gt "0" ]; then + echo "Not a valid sleep value: $SLEEP" + exit 1 + fi ;; h) echo "$USAGE" exit 0 ;; + \?) #echo "Invalid argument: $OPTARG" + exit 1 + ;; + :) + echo "Option $OPTARG requires an argument." + exit 1 + ;; esac done +shift $((OPTIND-1)) +[ -n "$*" ] && CONTEXT="$*" + if [ $(id -u) -eq 0 ]; then [ ! -n "$FILE" ] && FILE="/etc/vdcron.conf" LOG="/var/log/vdcron.log" @@ -52,13 +74,6 @@ else mkdir -p "$(dirname "$LOG")" && touch "$LOG" fi -if [ ! -f "$FILE" ]; then - echo "Your config file $FILE is not found" - echo "Your config file $FILE is not found" >> "$LOG" - exit 1 -fi - - if date --version >/dev/null 2>&1 ; then [ $DEBUG ] && echo Using GNU date GNU_DATE=1 @@ -92,7 +107,7 @@ epoch_to_fmt(){ fi } -echo -n "Starting at ..." >> "$LOG" +echo -n "Starting by -- $CONTEXT -- at ..." >> "$LOG" sleep $SLEEP echo " $(date)" >> "$LOG"