Linux Cron & Task Scheduling

Cron syntax, user vs system crontabs, /etc/cron.d vs /etc/crontab, environment pitfalls, systemd timers as the modern alternative, and debugging strategies. Essential for any infrastructure or backend role.

Overview

What cron is

Cron is the Unix daemon for scheduled command execution. The modern cron daemon on most Linux distributions is vixie-cron or cronie, and the syntax has been stable for decades.

A job's life is short and always follows the same path.

  • You write a crontab entry: five time fields and a command.
  • The cron daemon (vixie-cron or cronie) reads it and, when the time matches, starts the command.
  • The command runs under /bin/sh with a near-empty environment, not your login shell.
  • Whatever the command prints to stdout and stderr goes to local mail, or nowhere.

Steps 3 and 4 cause most of the trouble, as the gotchas below show.

Key concepts

  • crontab command
  • /etc/cron.d and system crontabs
  • Common cron expression patterns
  • systemd timer units
  • Debugging cron failures

Interview questions

  • Write a cron expression for: run a backup every weekday at 2:30 AM, and a report every Sunday at 8 AM. What file would you put this in and why?
  • A script runs perfectly when executed manually but fails silently in cron. Walk through your diagnosis.
  • You need to ensure a cleanup job on a fleet of 50 servers doesn't hammer a shared database all at once. How do you implement the schedule?
  • Compare cron and systemd timers — when would you choose one over the other?
  • How do Kubernetes CronJobs differ from Linux cron, and what are the key pitfalls?

Sign up free to read the full topic

More DevOps topics

Start free on Cariara · Coding problems · System design studio