Batch Command Runner
Run one command on a list of SSH hosts, sequentially, with clear output.
$ webtoolist --script cron-wrapper
Run a command from cron with a lock, timestamped log, and exit status.
cronlockautomationloggingDependencies: flock, date
#!/usr/bin/env bash
set -euo pipefail
name="${1:-}"
shift || true
(($# > 0)) || { echo "usage: $0 NAME COMMAND [ARGS...]" >&2; exit 1; }
log_dir="${LOG_DIR:-$HOME/.local/state/cron}"
mkdir -p "$log_dir"
log="$log_dir/$name.log"
lock="$log_dir/$name.lock"
exec 9>"$lock"
if ! flock -n 9; then
echo "$(date -Is) already running; skipping" >> "$log"
exit 0
fi
{
echo "=== $(date -Is) start: $* ==="
"$@"
status=$?
echo "=== $(date -Is) end: status=$status ==="
} >> "$log" 2>&1
exit "${status:-0}"
./cron-wrapper nightly-backup rsync -a /data/ /mnt/backup/data/
# crontab: 15 2 * * * /usr/local/bin/cron-wrapper nightly-backup /usr/local/bin/backup.sh
flock -n returns immediately when a previous run still holds the lock.Related scripts
Run one command on a list of SSH hosts, sequentially, with clear output.