r/devops7 • u/Alternative_Pin9598 • 13d ago
One script, one chore: every cron job exited 0 last night, and two still failed. Catch the backup that "finished" in 4 seconds with one anomaly score per job (real output)
Short one this week: one script, one chore, real output. (This sub is run by the SynapCores team; the script uses our database for the one step that matters.)
The chore
Every cron job exited 0 last night. Monitoring is green. Two of them still failed:
backup-db"finished" in 4 seconds and wrote 1.2 KB. It usually takes 40 minutes and writes about 18 GB. (Classic causes: the dump pointed at an empty replica, or a wrapper script swallowed the real error.)logrotateran on time but shipped 8.2 GB, 38x its usual. Something upstream started logging in a loop.
exit_code <> 0 catches neither. You only find out when you need the backup.
Step 1: log three numbers per job
Wrap each job so it appends one line: date, job, exit code, seconds, bytes written.
start=$(date +%s); pg_dump mydb > /backups/db.sql; code=$?
echo "$(date +%F) backup-db $code $(( $(date +%s) - start )) $(wc -c < /backups/db.sql | tr -d ' ')" >> /var/log/runs.txtReal line from that wrapper (with a stand-in command):
2026-11-11 backup-db 0 1 6For this post I generated 30 nights x 6 jobs (gen_runs.py, seeded, in the first comment) with the two failures above on the last night.
Step 2: one table, plus "how far from usual" for each job
CREATE TABLE job_runs (day TEXT, job TEXT, exit_code INTEGER, duration_s FLOAT, bytes_out FLOAT,
dur_ratio FLOAT, bytes_ratio FLOAT);The script loads the lines, then for each job divides tonight's numbers by that job's average, so 1.0 means a normal night for that job. A 4-second cert renewal is normal; a 4-second database backup is not.
SELECT job, AVG(duration_s), AVG(bytes_out) FROM job_runs WHERE day < '2026-11-10' GROUP BY job;
-- then per job:
UPDATE job_runs SET dur_ratio = duration_s / <avg>, bytes_ratio = bytes_out / <avg> WHERE job = '<job>';Step 3, the payoff: an anomaly model in the database, scored inside the SELECT
CREATE EXPERIMENT cron_exp AS SELECT dur_ratio, bytes_ratio FROM job_runs WHERE day <= '2026-11-10'
WITH (task_type = 'anomaly_detection', algorithms = ['isolation_forest'], max_trials = 5, random_seed = 42);
DEPLOY MODEL cron_model FROM EXPERIMENT cron_exp;
SELECT job, exit_code, duration_s, bytes_out, dur_ratio, bytes_ratio,
ANOMALY_SCORE('cron_model', dur_ratio, bytes_ratio) AS score
FROM job_runs WHERE day = '2026-11-10' ORDER BY score DESC;Output of python3 jobwatch.py check 2026-11-10:
exit_code <> 0 last night: 0 jobs
ODD backup-db exit 0 4s (0.00x usual) 1.2 KB ( 0.00x usual) anomaly 0.87
ODD logrotate exit 0 35s (0.87x usual) 8.2 GB (37.75x usual) anomaly 0.86
ok cert-renew exit 0 10s (0.81x usual) 48.5 KB ( 1.13x usual) anomaly 0.58
ok report-export exit 0 313s (1.03x usual) 615.1 MB ( 1.16x usual) anomaly 0.53
ok backup-files exit 0 1608s (1.05x usual) 7.2 GB ( 1.10x usual) anomaly 0.42
ok db-vacuum exit 0 908s (1.02x usual) 1.1 MB ( 1.00x usual) anomaly 0.41Both quiet failures on top, and a clear gap down to the rest. Is 0.8 going to page you every night? The top 5 scores over all 180 runs:
2026-11-10 backup-db 0.87
2026-11-10 logrotate 0.86
2026-10-13 db-vacuum 0.63
2026-11-10 cert-renew 0.58
2026-11-06 report-export 0.56In 30 nights, nothing else got past 0.63.
Why a model and not thresholds? You can write duration < 10% of usual. Then you need "bytes too low", "bytes too high", "too slow", for every job: 6 jobs x 2 numbers x 2 directions = 24 thresholds to tune and keep tuned. The model learns what normal looks like for both numbers at once, and you get one score per job, inside the same SELECT, with no extra service.
Run it from cron after the last job: python3 jobwatch.py check $(date -d yesterday +%F), and send the ODD lines wherever your alerts go.
Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-one-script-5
What's the worst "exit 0 but it didn't actually work" job you've found, and how long had it been broken?
1
u/Alternative_Pin9598 13d ago
jobwatch.py(standard library only; setSC_URLandSC_TOKEN; create thejob_runstable from Step 2 first):gen_runs.py(the seeded sample:python3 gen_runs.py > runs.txt && python3 jobwatch.py ingest runs.txt && python3 jobwatch.py check 2026-11-10):Picking the cut-off: SQLv2 Lesson 3 shows how to choose a score threshold from data instead of guessing, which is the same question as "is 0.8 right for my jobs": https://www.reddit.com/r/SQLv2/comments/1xkb3mn/
New to
ANOMALY_SCORE()? Recipe of the week #1 on r/synapcores walks through it from zero on a fraud example: https://www.reddit.com/r/synapcores/comments/1xlzo4t/