Cron jobs: the tiny line that runs half your backend
Every backend has a few jobs nobody looks at. The nightly backup. The 9 AM report. The script that deletes old files. Each one is a single line of cron. They work fine for months. Then one day you find out the backup hasn't run since March. Nobody noticed. This post is about that one line: what it means, where to run it, and how to hear about it when it stops. What cron actually is cron - a…
Cron jobs are small lines of code that run in the background on Unix machines. They perform tasks like backups, reports, and file deletions at specific times. Each job lives in a file called the crontab, which is edited using the command 'crontab -e'. This file contains all the scheduled commands, or cron jobs.
The syntax for cron jobs is a bit tricky. It's read from left to right: minute, hour, day of month, month, day of week, and then the command to be executed. Each field is a filter, and cron checks all five fields every minute. If all five say 'yes', the job runs. Simple symbols like '*' (every value), '1,15' (specific values), '1-5' (range), and '/15' (step) are used to define the schedule.
A common mistake is misunderstanding how cron handles ranges and intervals. For example, '9-17' means 'any minute where the hour is 9 to 17', which includes the whole 17th hour. To stop at exactly 17:00, you need to add an additional line: '0 17 * * 1-5'.
Another surprise is that cron has no memory. If the machine was off at a scheduled time, the job won't run later. It's also important to note that cron's timezone is always UTC unless specified otherwise.
Cron jobs can be run in various environments, including GitHub Actions, Docker, and Kubernetes. In each case, there are specific considerations to ensure the job runs correctly, such as timezone settings in GitHub Actions, environment variables in Docker, and job templates in Kubernetes.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.