Every server needs work to happen automatically. Log files need rotating at midnight, backups need running on Sunday, scripts need checking every few minutes. Nobody wants to wake up at 2 AM to run a backup command — and with Linux, you never have to.

That's the job of cron, the standard Linux task scheduler. It has been quietly running on almost every Unix system for decades, and learning it is one of the most practical skills a Linux user can pick up.

(And a quick note before we start: the command is crontab, not corntab — the "c" stands for chronos, time. It's one of the most common typos in Linux, so if you searched for that, you're in good company.)

What Is cron?

cron is a background service (a daemon) that runs time-based commands on a schedule. Think of it as an alarm clock that can run thousands of alarms at once — once per minute, it checks your schedule and runs anything that's due.

The two pieces to understand:

  • The cron daemon (crond / cron service) — runs in the background on the system, always. You rarely touch it; it just works.
  • The crontab file — your personal schedule. Each user, including root, has their own. Your commands go here.

Every scheduled task (called a cron job) is simply one line of text: when to run + what to run.

The crontab Syntax: Reading the Five Fields

Every cron job starts with five time fields, followed by the command:

* * * * * command-to-run | | | | | | | | | +-- Day of week (0-7, Sun=0 or 7, 1=Mon) | | | +---- Month (1-12) | | +------ Day of month (1-31) | +-------- Hour (0-23) +---------- Minute (0-59) 

Each field can contain:

Value Meaning
5 Exactly that value
* Every possible value (always)
*/10 Every 10 units (step values)
1,15,30 A list of values
9-17 A range of values

So the way you read a cron line is: the job runs when ALL five fields match the current time.

Common Examples Worth Memorizing

Real schedules you will actually need:

# Every day at 2:00 AM 0 2 * * * /home/yeasin/scripts/backup.sh # Every 15 minutes */15 * * * * /home/yeasin/scripts/check.sh # Every hour, on the hour 0 * * * * /home/yeasin/scripts/ping-monitor.sh # Every Monday at 9 AM 0 9 * * 1 /home/yeasin/scripts/report.sh # First day of every month at midnight 0 0 1 * * /home/yeasin/scripts/cleanup.sh # At boot (great for services and startup scripts) @reboot /home/yeasin/scripts/start-myapp.sh 

That last one, @reboot, is a handy shortcut that runs a command whenever the system starts — no five fields needed. There are a few of these strings: @reboot, @daily (midnight), @hourly, @weekly, @monthly.

Tip: Not sure if your schedule means what you think? Play with an interpreter like crontab.guru — paste the five fields in and it translates them into plain English. Very useful while learning.

Managing Your crontab

You never edit cron jobs by opening a file directly. You use the crontab command:

crontab -e # edit your cron jobs (opens your default editor) crontab -l # list your cron jobs crontab -r # remove ALL your cron jobs (careful — no confirm by default!) 

The first time you run crontab -e, pick an editor (choose 1 for nano — the friendly one). Then add jobs, one per line:

# m h dom mon dow command 0 2 * * * /home/yeasin/scripts/backup.sh */15 * * * * /home/yeasin/scripts/check.sh 

Save and exit — that's it. The daemon picks up your changes automatically. No restart, no reload needed. If you want to confirm:

sudo systemctl status cron 
● cron.service - Regular background program processing daemon Active: active (running) 

Warning: crontab -r deletes your entire schedule instantly, and unlike rm -i, there's no safe flag by default. A safer habit: always edit instead of remove, or first save a copy with crontab -l > ~/crontab-backup.txt.

Editing Other Users' crontabs

With sudo, you can manage anyone's schedule:

sudo crontab -e -u rahim # edit rahim's jobs sudo crontab -l -u rahim # list rahim's jobs 

And for system-wide scheduled tasks, Linux also maintains separate files:

  • /etc/crontab — system cron file (note: it has an extra user column)
  • /etc/cron.d/ — drop-in files for packages and custom system jobs
  • /etc/cron.daily/, cron.hourly/ — just drop executable scripts in these folders

For everyday work though, crontab -e for your own user is what you'll use 95% of the time.

The Two Most Common cron Mistakes

These two trip up almost everyone — know them and you're ahead of most tutorials.

1. cron's environment is not your shell's environment

When you run a command in your terminal, it works because your shell knows the PATH, your aliases, your environment variables. cron knows almost none of that. It runs jobs in a minimal environment, with the user's home as the working directory, and a bare PATH.

This is why a script works in your terminal but mysteriously fails in cron. The fixes:

Tip: In cron jobs, always use absolute paths — for the script, and for every command inside it.

# Fragile — depends on cron's PATH 0 2 * * * backup.sh # Robust — absolute paths 0 2 * * * /usr/bin/bash /home/yeasin/scripts/backup.sh 

And inside the script:

#!/usr/bin/env bash # Set PATH yourself at the top of scripts run by cron PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

2. Never see the output? It's being emailed to nowhere

cron captures a job's output (including errors) and emails it to the local mailbox of the user. On most modern systems, there is no mail reader configured — so your script can be failing for weeks and you'd see nothing.

The fix is simple and professional: redirect output to a log file.

0 2 * * * /home/yeasin/scripts/backup.sh >> /home/yeasin/logs/backup.log 2>&1 

>> appends normal output to the log, and 2>&1 sends error messages there too. Now every run leaves a trace you can inspect:

cat ~/logs/backup.log tail -f ~/logs/backup.log 

This single habit — logging every cron job — is the difference between debugging in 2 minutes and debugging in 2 hours.

Verify a Job Actually Ran

cron only schedules; whether your command succeeded is another matter. Three checks:

# 1. Is the job listed? crontab -l # 2. Did cron attempt it? (system log evidence) grep CRON /var/log/syslog | tail # 3. What did it output? (your own log file) tail /home/yeasin/logs/backup.log 

The syslog line looks like this:

Oct 25 02:00:01 server CRON[4821]: (yeasin) CMD (/home/yeasin/scripts/backup.sh) 

If cron ran the command but it failed, this line says so anyway — the error lives in your log file. That's why step 2 and step 3 go together.

Note: On newer Ubuntu versions, logs may live in the journal instead: journalctl -u cron | tail.

A Practical Example: Automatic Database Backup

Let's put it all together into a realistic setup — a daily backup that actually tells you when it fails.

The script (~/scripts/backup-projects.sh):

#!/usr/bin/env bash set -u PATH=/usr/local/bin:/usr/bin:/bin LOG=~/logs/backup.log DEST=~/backups mkdir -p "$DEST" ~/logs echo "=== Backup started: $(date) ===" >> "$LOG" if tar -czf "$DEST/projects-$(date +%F).tar.gz" ~/web 2>> "$LOG"; then echo "Backup OK: $(date)" >> "$LOG" else echo "BACKUP FAILED: $(date)" >> "$LOG" fi 

Remember to make it executable — a very common experience-killing step:

chmod +x ~/scripts/backup-projects.sh 

The cron job — run crontab -e and add:

0 2 * * * /home/yeasin/scripts/backup-projects.sh 

Every night at 2 AM you get a timestamped backup, and any failure is written to your log. Test it once by hand (./scripts/backup-projects.sh), check the log, and only then trust it to cron.

Alternatives Worth Knowing (Briefly)

cron is the standard, but two siblings show up a lot:

  • systemd timers — the modern alternative on recent Debian/Ubuntu. More powerful (dependencies, better logging) and integrates with journalctl. More configuration, though.
  • anacron — for machines that aren't always on (laptops, desktops). If the computer was off at the scheduled time, it runs the job when the machine wakes. cron simply misses jobs while the system is powered off.

Rule of thumb: servers → cron or systemd timers; personal laptops → anacron if missing scheduled runs matters to you.

Summary

  • The daemon is cron, the schedule file is crontab — each user has their own, managed with crontab -e / -l.
  • Every job is five time fields plus a command: 0 2 * * * command.
  • @reboot schedules startup tasks; crontab -r wipes your entire schedule — treat it with respect.
  • cron runs jobs in a minimal environment: use absolute paths everywhere.
  • Always redirect output to a log file (>> file 2>&1) — otherwise failures vanish silently.
  • Verify with crontab -l, grep CRON /var/log/syslog, and your own logs.
  • Make scripts executable with chmod +x, and test them manually before trusting the schedule.

Automating your first backup or monitoring script is a milestone — it's the moment your server starts working for you, even while you sleep.