414a990801
The single-timer threshold monitor generalizes into a scheduler: each job is a
chmod-600 file in schedule.d/<name>.env (INTERVAL 5m..59m/1h..23h/hourly/daily/
weekly/OnCalendar=..., NOTIFY policy, optional MSG, RULE for threshold,
COMMAND = literal rest of line) with its own systemd user timer pair
(pos-schedule-<name>.timer + oneshot .service, Persistent, reconciled on
enable/disable — orphan units + the legacy pos-event-trigger timer
auto-removed). Policies: always (full output every run), onchange (diff vs
last run, first run sends), onerror (non-zero exit or empty output),
threshold (old event-trigger behavior: first numeric vs RULE, alert on
false→true + recovery, per-job firing state), never (silent side-effect jobs).
run [name|all], list, config (interactive add/edit/remove/enable/disable with
validation), enable/disable [name|all], status, migrate (converts legacy
event.env rules → rule-N.env threshold jobs, verbatim LHS as COMMAND, adopts
the legacy timer's OnCalendar or 5m, removes the old timer). Per-run logs +
state in ~/.local/share/linux_post_install/schedule/{logs,state}/.
config/event.env + event-rules.template → config/schedule.d/ starter jobs
(nvme-health, cpu-temp, disk-root, silent log-cleanup); postinstall installs
them no-clobber into an empty schedule.d/ (legacy event.env users get a
migrate hint instead). bin/pos EXAMPLES + INTERACTIVE_CMDS
(system-schedule config) updated; install.sh ships the renamed tool+lib.
82 lines
3.0 KiB
Bash
Executable File
82 lines
3.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
# POS: system schedule — Scheduled jobs: run a command on a timer; notify on threshold/change/error/always or silently
|
|
# POS_SUBCMDS: run list config enable disable status migrate
|
|
# POS_FLAGS: --dry-run
|
|
|
|
source "$(dirname "$0")/../lib/common.sh" 2>/dev/null || source "$(dirname "$0")/common.sh"
|
|
source "$(dirname "$0")/../lib/scheduler-lib.sh" 2>/dev/null || source "$(dirname "$0")/scheduler-lib.sh"
|
|
source "$(dirname "$0")/../lib/notify.sh" 2>/dev/null || source "$(dirname "$0")/notify.sh"
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
Usage: pos system schedule <subcommand> [--dry-run]
|
|
|
|
Scheduled jobs. Each job is a file in $SCHEDULE_DIR/<name>.env:
|
|
|
|
INTERVAL=hourly (5m..59m | 1h..23h | hourly daily weekly | OnCalendar=…)
|
|
NOTIFY=onchange (always | onchange | onerror | threshold | never)
|
|
MSG="NVMe health" optional label; threshold alert text when NOTIFY=threshold
|
|
RULE="> 60c" threshold only: op + threshold (unit suffix fine: 60c, 80%)
|
|
COMMAND=… literal rest of the line — pipes/quotes/sudo fine
|
|
|
|
NOTIFY policies:
|
|
always send the full output on every run
|
|
onchange send only when the output differs from the last run (first run sends)
|
|
onerror send only when the command fails (non-zero exit or empty output)
|
|
threshold compare the command's first numeric output with RULE; alert on
|
|
false→true plus one recovery message
|
|
never run only — no notification (side-effect jobs)
|
|
Default: threshold if RULE is set, otherwise onchange.
|
|
|
|
Subcommands:
|
|
run [name|all] Execute job(s) now (the systemd timers call 'run <name>')
|
|
list Jobs + notify policy + interval + last run
|
|
config Interactive editor: add / edit / remove / enable / disable
|
|
enable [name|all] Install + start the job's user timer (default: all)
|
|
disable [name|all] Stop the job, keeping the job file (default: all)
|
|
status Per-job timer state + next run
|
|
migrate Convert legacy event.env rules into schedule.d jobs (threshold)
|
|
|
|
Options:
|
|
--dry-run Preview what would run / be written / be sent
|
|
-h, --help This help
|
|
|
|
Jobs dir: $SCHEDULE_DIR (chmod 600 per file)
|
|
|
|
Examples:
|
|
pos system schedule list
|
|
pos system schedule enable
|
|
pos system schedule run nvme-health --dry-run
|
|
pos system schedule migrate
|
|
EOF
|
|
exit 0
|
|
}
|
|
|
|
case "${1:-}" in
|
|
-h|--help|"") usage ;;
|
|
esac
|
|
|
|
DRY_RUN=0
|
|
args=()
|
|
for a in "$@"; do
|
|
case "$a" in
|
|
--dry-run) DRY_RUN=1 ;;
|
|
*) args+=("$a") ;;
|
|
esac
|
|
done
|
|
set -- "${args[@]}"
|
|
|
|
[ -d "$SCHEDULE_DIR" ] || log "no jobs yet — add one with 'pos system schedule config'"
|
|
|
|
case "${1:-}" in
|
|
run) sched_run "${2:-all}" ;;
|
|
list) sched_list ;;
|
|
config) sched_config_editor ;;
|
|
enable) sched_enable "${2:-all}" ;;
|
|
disable) sched_disable "${2:-all}" ;;
|
|
status) sched_status ;;
|
|
migrate) sched_migrate ;;
|
|
*) err "unknown subcommand '${1:-}' (see --help)" ;;
|
|
esac
|