What is a cron job, and how do you schedule tasks on a server?
A cron job is a command a server runs automatically on a schedule, like backing up a database every night or cleaning up temporary files every hour. The name comes from cron, the scheduler built into Unix-like systems.
How the schedule is written. Each line in a crontab has five time fields followed by the command:
minute, hour, day of month, month, day of week
So 0 3 * * * means "at 3:00 every day," and */15 * * * * means "every 15 minutes." Online crontab helpers can translate expressions into plain language, which is handy while learning.
Getting started:
- Run
crontab -eto edit your user's scheduled jobs. - Add a line with the schedule and the full command.
- Save; cron picks it up automatically.
Beginner mistakes:
- Assuming the same environment as your terminal. Cron runs with a minimal environment, so use full paths to programs and files.
- Not capturing output. Redirect output to a log file so you can see errors.
- Time zone confusion. The server's time zone may not be yours.
- Overlapping runs. If a job takes longer than its interval, copies can pile up. Use a lock.
- No alert on failure. A silently failing backup job is worse than none.
In the cloud, you'll also find managed schedulers and scheduled serverless functions that do the same job without a server to maintain.
What's the first task you automated with cron? Do you use a monitoring service to confirm scheduled jobs actually ran?
Comments · 3
Remember cron runs with a minimal environment, so use full paths and log the output. Most "it works by hand but not in cron" bugs come from that.
What happens if the server is off when a job is scheduled? Does it run later or just get skipped?
Standard cron skips it. Look at anacron or a systemd timer with Persistent=true if you need missed jobs to run on next boot.