Skip to main content

Linux and servers · Beginner · 10 min

How to Schedule Jobs with Cron on Linux

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · cron

You will schedule a command to run every minute, then on a real timetable, read the cron syntax, and learn why a job that works in your shell can fail under cron.

On this page

Cron runs commands at set times: a backup at 02:00, a report every Monday, a clean-up every hour. It has been part of Unix for decades and every Ubuntu server can run it. In this tutorial you will install it, schedule a job, read the five-field time syntax, and avoid the usual reasons a job runs by hand but not from cron.

Step 1 · Install cron and start it

Ubuntu server images include cron. Check, and install it if it is missing:

Bash
sudo apt-get update
sudo apt-get install -y cron

Once installed, the cron service is enabled and running. Confirm it:

Bash
systemctl status cron

You want Active: active (running). If it is not, run sudo systemctl enable --now cron.

Step 2 · Read the schedule syntax

A cron line has five time fields followed by the command:

Text
┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-7, where 0 and 7 are Sunday)
│ │ │ │ │
* * * * *  command to run

Each field can hold:

  • * for every value.
  • A number, such as 5.
  • A list, such as 1,15.
  • A range, such as 1-5.
  • A step, such as */10 for every ten.

Some examples:

| Line starts with | Meaning |
|---|---|
| * * * * * | every minute |
| */15 * * * * | every 15 minutes |
| 0 2 * * * | at 02:00 every day |
| 30 6 * * 1-5 | at 06:30, Monday to Friday |
| 0 0 1 * * | at midnight on the first of each month |
| @reboot | once, when cron starts at boot |

Cron also accepts @hourly, @daily, @weekly, @monthly and @yearly as shortcuts. They run at minute 0 of the period.

Times use the server's time zone. Check it with timedatectl. Most servers run on UTC.

Step 3 · Add a job with crontab

Each user has a personal crontab. crontab -e opens it in your editor and installs it when you save. The editor is chosen the first time, and sudo select-editor changes it later.

Bash
crontab -e

For a first job, write the date to a file every minute. Before you start, look at what is already scheduled:

Bash
crontab -l

If it prints no crontab for <user>, the table is empty. The next command installs a whole new table from standard input, and replaces any jobs you have. If crontab -l shows jobs, save them first with crontab -l > ~/crontab.backup, or add the line below with crontab -e instead.

Bash
echo '* * * * * date >> /tmp/cron-test.log 2>&1' | crontab -
crontab -l

crontab -l prints the installed table. Cron reads the change within a minute, so no restart is needed. In a real crontab you can also set variables at the top, such as MAILTO="" to stop cron emailing the output of every run.

Wait for the minute to turn over, then read the log:

Bash
cat /tmp/cron-test.log

Check it worked

The log holds one or more date lines, for example Sun Nov 15 04:31:01 UTC 2026. Each line is one run. Remove the job when you are done. Open the table with crontab -e and delete the line.

Bash
crontab -e

If the table was empty before this tutorial, crontab -r removes it all. It asks for no confirmation, so do not use it on a table with other jobs.

Step 4 · Write a job that survives cron's environment

Cron starts jobs with a small environment and the home directory as the working directory. A command that works in your shell may fail under cron because it relies on PATH entries, variables or a directory it is no longer in.

Prove it by printing what cron gives a job:

Bash
echo '* * * * * env > /tmp/cron-env.log 2>&1' | crontab -

After a minute, read the file, and then remove the job as before:

Bash
cat /tmp/cron-env.log

The output has a short PATH (/usr/bin:/bin), a HOME, a SHELL of /bin/sh and few other variables. The same job in your login shell would show dozens. From this follow four habits:

  • Use full paths: /usr/bin/python3 /home/deploy/report.py, not python3 report.py.
  • Set PATH at the top of the crontab if a job needs more than /usr/bin:/bin: PATH=/usr/local/bin:/usr/bin:/bin.
  • Change directory inside the command: cd /home/deploy/app && ./backup.sh.
  • Send output to a file you can read: >> /var/log/myjob.log 2>&1. Without this, the output goes to local mail, which most servers do not read.

The shell is /bin/sh, not bash. Add SHELL=/bin/bash at the top if the command uses bash-only features.

Troubleshooting

  • The job never runs: check the syntax with crontab -l, make sure the file ends with a newline, and check that the script is executable (chmod +x).
  • It runs but does nothing: read the log you redirected to. The usual cause is a command that cannot be found, which means a full path is missing.
  • % characters: in a crontab line, an unescaped % means a newline. Write date +\%F, not date +%F.
  • Find out whether cron ran it at all: on Ubuntu, grep CRON /var/log/syslog or journalctl -u cron.

Step 5 · System-wide jobs and avoiding overlap

Besides user crontabs, cron reads /etc/crontab and the files in /etc/cron.d/. These lines have a sixth field, the user to run as, between the time and the command.

Bash
echo '0 3 * * * root /usr/bin/date >> /var/log/cron-demo.log' | sudo tee /etc/cron.d/demo
sudo chmod 644 /etc/cron.d/demo
sudo cat /etc/cron.d/demo

This example only appends the date to a log at 03:00. Delete it when you have seen how it works: sudo rm /etc/cron.d/demo.

Put a file name in /etc/cron.d/ without a dot, because cron ignores names that contain dots. For simple schedules, drop an executable script into /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly or /etc/cron.monthly, with no extension in the name.

If a job can take longer than its interval, two copies run at once. Wrap it in flock, which refuses to start a second copy:

Bash
flock --nonblock /tmp/backup.lock -c 'echo "got the lock"'

In a crontab it looks like */5 * * * * flock -n /tmp/backup.lock /home/deploy/backup.sh.

Next steps

  • Run a long-lived app instead of a timed one: /tutorials/run-an-app-as-a-systemd-service.
  • Make the scripts you schedule safe with correct ownership: /tutorials/linux-file-permissions-chmod-chown.
  • Find out what a scheduled clean-up can reclaim in /tutorials/find-what-is-using-disk-space-linux.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.