Skip to lesson

Linux Command Line · Lesson 31 of 43

Scheduling tasks with cron

Run commands automatically on a schedule with cron: read and write the five time fields, edit your crontab, handle paths and logs, and avoid common traps.

Beginner to intermediate·12 min·Processes and the system·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will make a command run by itself: a backup every night, a cleanup every week, a report every Monday morning. Cron is the classic Unix scheduler, available on almost every Linux server. Changing the schedule of a real machine affects it for good, so the crontab commands below are shown as text and not run in our practice environment.

How cron works

cron is a service that wakes up every minute, reads the schedules of all users and starts whatever is due. Each user has a crontab (cron table), a text file with one job per line. You never edit it directly; use the crontab command:

text
crontab -l      show your crontab
crontab -e      edit it in your editor
crontab -r      remove it (no confirmation: be careful)

Saving the file from crontab -e installs the new schedule at once.

The five time fields

A job line has five time fields and then the command:

text
┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-7, Sunday is 0 or 7)
│ │ │ │ │
* * * * *  command to run

An asterisk means "every". Examples:

Schedule Meaning
* * * * * Every minute
30 2 * * * 02:30 every day
0 9 * * 1 09:00 every Monday
0 0 1 * * Midnight on the first day of every month
*/15 * * * * Every 15 minutes
0 8-18 * * 1-5 On the hour from 08:00 to 18:00, Monday to Friday
0 6,18 * * * At 06:00 and 18:00 every day

Lists use commas, ranges use dashes, and steps use a slash. If you restrict both the day of month and the day of week (neither is *), cron runs the job when either matches, which surprises people. Special strings exist for common cases: @reboot, @hourly, @daily, @weekly and @monthly.

Working out the fields

If you are not sure which numbers a date produces, the date command can print them in cron's order. This shows the fields for 10:00 on 15 January 2026, which is a Thursday:

bash
date -d "2026-01-15 10:00" +"%M %H %d %m %u"
result
00 10 15 01 4

Minute 00, hour 10, day 15, month 01, weekday 4 (Thursday, where Monday is 1). The -d option is GNU date; on macOS use date -j -f instead. Websites such as crontab.guru explain any expression in words, and they are good for checking your work.

A complete entry

Here is a realistic crontab with a comment, a variable and two jobs:

text
#m  h  dom mon dow  command
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""

30 2 * * *  /home/asha/bin/backup.sh >> /home/asha/backup.log 2>&1
0 9 * * 1   /usr/bin/python3 /home/asha/reports/weekly.py

The first job runs a backup script at 02:30 every night and appends everything it prints, including errors, to a log file. The second runs a Python script every Monday morning. Lines starting with # are comments.

Why cron jobs fail

Most failures come from the same few causes.

A minimal environment. Cron does not read ~/.bashrc or your login files. Its PATH is short, usually /usr/bin:/bin (the Debian and Ubuntu manual pages state exactly that; other systems may add a few folders). Use full paths to commands and scripts, or set PATH at the top of the crontab as shown above.

The wrong working directory. A job starts in your home folder, not in your project. In a script, use absolute paths or cd first: cd /home/asha/project && ./build.sh.

The percent sign. In a crontab, % is special and means a newline. Escape it inside commands as \%, for example date +\%F.

No execute permission. A script must be executable, or run it with bash /path/script.sh.

Lost output. Cron tries to email the output to you, which usually goes nowhere. Redirect to a log with >> file 2>&1, as above, so you can see what happened.

Overlapping runs. If a job takes longer than the interval, two copies may run at once. Wrap it with flock, which holds a lock file: flock -n /tmp/backup.lock /home/asha/bin/backup.sh.

Testing a job

Do not wait for the clock. Run the exact command by hand with a minimal environment, which reproduces what cron sees:

text
env -i /bin/sh -c '/home/asha/bin/backup.sh'

Or schedule it every minute while testing, check the log, and change it to the real schedule afterwards. On many systems, the cron service logs each run to the system log: look in /var/log/cron (Fedora and Red Hat family) or run journalctl -u cron (the service is called crond on Fedora and Red Hat systems, so use journalctl -u crond there).

System-wide schedules

Besides personal crontabs, administrators use /etc/crontab and the folders /etc/cron.daily, /etc/cron.hourly and /etc/cron.weekly, where any executable script is run on that schedule. /etc/cron.d/ holds files with an extra column for the user name.

Other schedulers

at runs a job once, at a time you name, as in echo "backup.sh" | at 23:00. systemd timers are the modern alternative on many distributions: they offer logging with journalctl, dependencies and missed-run catch-up (Persistent=true). On a desktop or laptop that is switched off at night, anacron makes sure jobs still run when the machine is next on.

Common mistakes

Relying on your login environment. Use absolute paths.

Testing only in the terminal. Test with a clean environment too.

Forgetting to log. A job that fails silently is worse than one that never ran.

Typing crontab -r by accident. The r is next to the e on the keyboard, and the whole crontab disappears without a prompt. Keep a copy with crontab -l > crontab.backup.

Exercise

Write the five fields for each of these: every weekday at 07:45, every 10 minutes, the first of January at midnight, and every Sunday at 23:30. Check them with a cron explainer. Then write a full crontab line that runs /home/you/bin/cleanup.sh every night at 03:15 and appends its output to a log file.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 What does the crontab schedule 0 9 * * 1 mean?
02 Which command opens your personal crontab for editing?
03 Why do cron jobs often fail with 'command not found' even though they work in your terminal?
04 What does */15 in the minute field mean?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses