DEV Community

Cover image for Keep your system healthy with 20 cron jobs every DevOps engineer needs.
<devtips/>
<devtips/>

Posted on

Keep your system healthy with 20 cron jobs every DevOps engineer needs.

Stop babysitting your servers. These cron jobs will keep your infra clean, safe, and drama-free so you can finally sleep through the night.

You know that one server that always acts up at 2 AM like it just remembered it has feelings? Yeah. That’s why cron exists.

If you’re in DevOps or managing infra even part-time, cron jobs are your quiet heroes. They don’t ask for much. No dashboard. No fancy UI. Just raw automation, scheduled down to the minute, quietly keeping your systems alive and sane.

In this post, we’ll walk through 20 practical, must-have cron jobs the kind real engineers use in production. These aren’t gimmicks or “top 10 hacks for Linux.” They’re the jobs that have saved servers, dev teams, and sleep schedules. I’ll explain each one like I’m handing you my cheat sheet, not pitching a product.

Let’s start with the basics: keeping your system healthy.

System health & monitoring (6 cron jobs)

1. Check server uptime

/5 * * * * uptime >> /var/log/uptime.log

If your server’s randomly rebooting, this job logs uptime every 5 minutes. You can grep this later to debug shady downtime without digging through 15 logs.

2. Clean up disk space

0 3 * * * find /tmp -type f -atime +3 -delete

Disk fills up with temp trash? This nukes unused /tmp files older than 3 days. Run it at 3 AM when no one’s around to complain.

3. Rotate and compress logs

0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf

Your logs will grow until they eat the server. This cron job rotates and compresses logs daily. Logrotate handles most formats and keeps things tidy.

4. Monitor memory usage

/10 * * * * free -m >> /var/log/mem.log

Logs memory usage every 10 mins. Great for spotting leaks in that one legacy app that swears it’s “just caching.”

5. Watch for high CPU usage

/5 * * * * top -b -n1 | head -n 20 >> /var/log/cpu.log

Top 20 CPU processes, logged every 5 mins. This job lets you catch runaway processes before they start melting your instance.

6. Check disk usage and alert

0 * * * * df -h | mail -s "Disk Report" you@example.com

Hourly disk report emailed to you. Set an alias for your SRE team. Bonus points if it emails Slack via webhook.

Backup & disaster recovery (5 cron jobs)

Your infra is only as good as your last backup. And your last backup? Probably broken unless you’ve actually tested it. These cron jobs keep your data alive and recoverable even after someone accidentally rm -rf's prod (yes, it happens).

7. Daily database backup (MySQL/PostgreSQL)

0 2 * * * pg_dump yourdb | gzip > /backups/yourdb_$(date +\%F).sql.gz

Runs at 2 AM daily. Swaps pg_dump with mysqldump if you’re using MySQL. Gzips the dump and names it with the date. Add offsite sync next.

8. Sync backups to offsite storage (like S3)

0 3 * * * aws s3 sync /backups s3://your-bucket-name

Once the local backup’s ready, this cron job pushes it to S3. Because local-only backups are like saving your game… and then deleting the console.

9. Backup your config files

30 1 * * * tar -czf /backups/configs_$(date +\%F).tar.gz /etc/nginx /etc/docker

Don’t just back up data backup your nginx, Docker, and other config directories. If a server dies, you’ll want these back fast.

10. Snapshot the entire VM (if on cloud provider)

Depends on your provider, but for AWS:

0 4 * * * aws ec2 create-snapshot --volume-id vol-xxxxxx --description "Daily snapshot"

Set up IAM roles and snapshot policies first. This cron job can snapshot your EBS volume daily. Combine with a retention policy to avoid $urprise bills.

11. Verify backup integrity

0 5 * * * gzip -t /backups/.gz || echo "Backup corrupted!" | mail -s "Backup failed" you@example.com

This silently checks if your backup gzip files are valid. If corrupted, you get an angry email. Like a boss yelling, but helpful.

Press enter or click to view image in full size

Security & patching jobs (5 cron jobs)

You can’t ship fast if you’re busy cleaning up after the last breach. These jobs reduce your attack surface without becoming a full-time SOC.

12. Apply security updates

0 4 * * * apt update && apt upgrade -y

Run daily or weekly depending on risk tolerance. This won’t catch kernel-level stuff, but it’ll patch 90% of known CVEs before coffee.

13. Scan for malware

0 2 * * * clamscan -r / | grep FOUND >> /var/log/clamav.log

Run ClamAV recursively and log only what’s suspicious. You don’t need to scan daily, but weekly is smart. Especially on shared or older servers.

14. Check for SSL certificate expiry

0 0 * * * openssl x509 -enddate -noout -in /etc/ssl/certs/your-cert.pem | cut -d= -f2

Add logic to alert when it’s <10 days left. Because no one likes waking up to a “NET::ERR_CERT_EXPIRED” Slack fire.

15. Monitor failed SSH logins

/30 * * * * grep "Failed password" /var/log/auth.log | tail -n 10

Run every 30 minutes and log tail output. If bots are hammering you, this job gives you receipts before you need to block IPs.

16. Cron job audit

0 6 * * * crontab -l >> /var/log/cron_audit.log

Because sometimes it’s not attackers it’s other engineers adding cron jobs at 3 AM and forgetting. Audit your own schedule before it surprises you.

Developer & app workflow jobs (4 cron jobs)

Not every job is about saving the server. Some are just about saving your sanity (and maybe your CI/CD bill). These cron jobs clean, reset, and auto-fix the parts devs forget exist.

17. Auto-restart failed services

/10 * * * * systemctl is-active your-app || systemctl restart your-app

Every 10 mins, check if your service is running. If not, restart it. Like a watchdog, but simpler and no bloat.

18. Clean Docker logs

0 1 * * * truncate -s 0 /var/lib/docker/containers//-json.log

Docker logs grow like a memory leak with commitment issues. This job clears them daily. Careful: you’ll lose logs, so send them elsewhere first.

19. Clean old CI/CD build artifacts

0 3 * * * rm -rf /var/lib/jenkins/workspace//builds//archive/

You’d be shocked how much Jenkins hoards. This nukes stale build files. Replace with whatever fits your CI tool.

20. Mirror Git repos nightly

0 2 * * * git clone --mirror https://github.com/your/repo.git /backups/repo.git

Nightly Git mirror to a backup location. Useful when GitHub goes down or someone rage-pushes main without review.

Conclusion: set it, test it, forget it

The best cron jobs are the ones you forget even exist because they just work.

This list isn’t about showing off obscure bash tricks. It’s the stuff real DevOps engineers actually set up in prod to avoid weekend outages, Slack fires, and “who touched the server?” postmortems.

Start with the critical ones: backups, uptime checks, and log cleanup. Test them in staging (seriously, don’t rm -rf on prod), add some email alerts, and then slowly build your cron arsenal.

In the end, cron won’t win awards but it will keep your infra alive while you’re watching Netflix or grinding ranked matches. Which is kind of the point.

Helpful resources

Press enter or click to view image in full size

Top comments (0)