feat(varaverk): append logs per run with timestamp separator
run_job.sh was overwriting the log on every cron fire, making it impossible to see run history. Switch to append mode, print a ── YYYY-MM-DD HH:MM:SS ── separator before each run, and trim to the last 1000 lines after each run to keep logs bounded.
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
# Usage: bash run_job.sh <job_id> <script_path> [flags...]
|
||||
#
|
||||
# Writes: /var/log/varaverk/<id>.json — status, timestamps, exit code, pid
|
||||
# /var/log/varaverk/<id>.log — latest run output (overwritten each run)
|
||||
# /var/log/varaverk/<id>.log — appended per run, trimmed to LOG_MAX_LINES
|
||||
#
|
||||
# Status values: running → ok (exit 0) | warn (exit 1) | error (exit 2+)
|
||||
|
||||
@@ -17,6 +17,7 @@ LOG_DIR="/var/log/varaverk"
|
||||
BASE="${JOB_ID%.sh}"
|
||||
LOG_FILE="$LOG_DIR/$BASE.log"
|
||||
STAT_FILE="$LOG_DIR/$BASE.json"
|
||||
LOG_MAX_LINES=1000
|
||||
|
||||
mkdir -p "$(dirname "$LOG_FILE")" "$(dirname "$STAT_FILE")"
|
||||
|
||||
@@ -24,7 +25,10 @@ START=$(date +%s)
|
||||
printf '{"id":"%s","status":"running","start":%s,"pid":%s}\n' \
|
||||
"$JOB_ID" "$START" "$$" > "$STAT_FILE"
|
||||
|
||||
bash "$SCRIPT" "$@" > "$LOG_FILE" 2>&1
|
||||
printf '\n── %s ────────────────────────────────────────────────\n' \
|
||||
"$(date '+%Y-%m-%d %H:%M:%S')" >> "$LOG_FILE"
|
||||
|
||||
bash "$SCRIPT" "$@" >> "$LOG_FILE" 2>&1
|
||||
EC=$?
|
||||
|
||||
END=$(date +%s)
|
||||
@@ -36,4 +40,10 @@ fi
|
||||
printf '{"id":"%s","status":"%s","start":%s,"end":%s,"exit":%s}\n' \
|
||||
"$JOB_ID" "$STATUS" "$START" "$END" "$EC" > "$STAT_FILE"
|
||||
|
||||
# Trim to last LOG_MAX_LINES to prevent unbounded growth
|
||||
line_count=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)
|
||||
if [ "$line_count" -gt "$LOG_MAX_LINES" ]; then
|
||||
tail -n "$LOG_MAX_LINES" "$LOG_FILE" > "${LOG_FILE}.tmp" && mv "${LOG_FILE}.tmp" "$LOG_FILE"
|
||||
fi
|
||||
|
||||
exit $EC
|
||||
|
||||
Reference in New Issue
Block a user