How do you handle cron jobs that silently stop working?
from indidev@lemmy.world to programming@programming.dev on 10 Jul 09:42
https://lemmy.world/post/49262598

Had a backup script running via cron for months. Worked fine until it didn’t — turns out the disk filled up three weeks ago and the job started failing silently. Nobody noticed until we actually needed a restore.

The obvious answer is “check your logs” but let’s be honest, nobody’s reading cron logs daily for 15 different scheduled tasks across 4 servers.

What’s your setup for making sure crons are actually completing? Do you just grep logs periodically, or do you have something more structured? Curious how others handle this without turning it into a whole project.

#programming

threaded - newest

Kissaki@programming.dev on 10 Jul 09:52 next collapse

If it were critical I would set up an “email me on script run failure” wrap around the individual cron jobs. Simple exit code check, potentially with console output as email body is simple and useful.

Scipitie@lemmy.dbzer0.com on 10 Jul 09:59 next collapse

Plus one dir a simple alert wrapper. Although for me it’s even more simple: the only cron stuff I have is backup and individual service updates and both have an entry on my Homepage Dashboard with a health indicator. Red dot == shit.

The only fancy custom thing I’ve built is not even cron based: sometimes I missed on my nixos server that I needed a reboot because kernel modules updated as well - so my nushell prompt compares the live environment with the loaded kernel - if versions mismatch my shell (rightfully) flames me to pay more attention and that I need to reboot.

For anything from based I’d use a wrapper - for me it would be a simple curl to the localhost matrix bridge so that I’d get a push message but for others it’s Mail or just a ntfy - whatever is easiest for your server to reach you, really. I would not want to check on my automations and the notification service id notice within two hours if it’s down because I use it for a lot of other items as well (no mails, appointments, reminders or events in 2 hours??)

cybervegan@lemmy.world on 10 Jul 10:01 next collapse

Monitor your logs with a network monitoring tool like Nagios, Icinga, etc. You can set filters for alerts you are genuinely interested in, and email the alerts to your ticketing system or sysads.

Mertn33@lemmy.world on 10 Jul 12:34 collapse

Graylog is excellent for catching all the logs from different system and filtering them according to your tastes into email alerts.

mbirth@lemmy.ml on 10 Jul 10:18 next collapse

Do you know about the MAILTO= lines in crontabs? That’s exactly what they’re there for. (And your script needs to output error messages on failure, of course.) You’ll need a local mail forwarder like ssmtp or exa, though.

[deleted] on 10 Jul 10:54 collapse
.
okwhateverdude@lemmy.world on 10 Jul 10:50 next collapse

Everyones’ suggestion to use MAILTO is great. Wrappers are another option. One more is just schedule another cron that checks to make sure the first one did what it is supposed to do and notify you if it broke.

ultimate_worrier@lemmy.dbzer0.com on 10 Jul 11:07 next collapse

Just as in life you shouldn’t say “can’t”, in programming, you shouldn’t say, “started failing silently”.

epyon22@sh.itjust.works on 10 Jul 11:11 next collapse

If you’ve got a good amount github.com/healthchecks/healthchecks

[deleted] on 10 Jul 11:32 next collapse
.
Shadow@lemmy.ca on 10 Jul 12:57 next collapse

I think the real obvious question is, why aren’t you monitoring your disk space?

kibiz0r@midwest.social on 10 Jul 15:08 collapse

Can’t answer for them, but I can explain a similar situation I ran into:

Large external RAID I was using for telescoping backups with a restic cron job. Didn’t want to have it on all the time cuz it was noisy, so I put it on a smart plug and had it turn on and off as part of the job.

I really wanted a set of backup rules that worked like… “always a full snapshot of current state, plus as much of the following as will fit into the destination: daily for the past 7 days, weekly for the past 4 weeks, monthly for the rest until full” …but restic doesn’t (or didn’t) support that kind of dynamic rule set.

I tried to estimate it programmatically, but it never worked 100% and I frequently ran into out of space errors.

But of course, I never saw the actual disk usage in my day-to-day, because the RAID was only on at like 4 AM.

Sxan@piefed.zip on 10 Jul 13:20 next collapse

I really like ntfy or gotify for stuff like þis. Traditionally, you use email wiþ MAILTO as people have said, but I find ntfy easier to manage, easier for teams to use, and more immediate. Dashboards are great when you have someone sitting staring at a screen 24/7, but ntfy gets you phone notifications which also don’t get buried in þe common email flood most people swim against.

cuckmaster69@lemmy.billiam.net on 10 Jul 15:26 next collapse

i have a simple python script to push a phone notification with all the relevant error info and which system is borked, using pushover.net

oranki@nord.pub on 10 Jul 16:07 collapse

Put the job in a script that only outputs text on error, and use MAILTO, like many have said. Due to spam filters that might not be an option though.

Next option is ntfy or gotify. make sure the script exits with nonzero status on error, and append || curl -XPOST -d 'cronjob failed' 'https://ntfy.sh/your-random-topic' to the crontab line. Naturally you’ll likely want the ntfy/gotify mobile app to receive the notodication. On iOS this might suck, as at least for me the ntfy app didn’t update in the background at all.

Next one is an push uptime monitor, self-hosted or managed. I like Uptime Kuma. Make sure the script only exits with 0 status on success, and append && curl 'https://your-monitor-endpoint/identifier to the crontab line. Or use an inverted monitor and use || instead of &&.

The uptime monitor will then notify you if the script hasn’t succeeded in the defined interval.

With some combination of these you can be pretty confident you’ll notice a failure, but still better to check every once in a while.

I also have a lot of jobs around that notify for each run, despite success or error, but it’s a bit too easy to just glance the message and accidentally ignore an error.