UnixTimestamp.app

Crontab Linux Basics: Create, List, Edit and Remove Cron Jobs

To add a job with crontab on Linux, run crontab -e, write one line with five time fields followed by a command, then save. Cron reads the change automatically, so you never need to restart it. This guide covers how to create, list, edit and remove cron jobs safely.

Matthias LöfflmannPublished · 7 min read
Linux server with a terminal holding a crontab line linked to a clock that triggers scheduled cron jobs
Key takeaways The short version, before the details.
  1. Run crontab -e, add a line such as 30 2 * * * /path/to/script.sh, save and quit.
  2. Use crontab -l to list jobs and be careful with crontab -r, which deletes everything without asking.
  3. /etc/crontab and /etc/cron.d need an extra user field; personal crontabs do not.
  4. @reboot, @daily and the cron.daily folders cover common schedules without writing five fields.

How to create a crontab job in Linux

Every user on a Linux system can have a personal crontab: a small table of commands and the times they should run. You never edit the file directly. Instead, the crontab command opens a copy in your editor and installs it when you save.

Open your crontab:

crontab -e

The first time, some distributions ask which editor to use. Add one line at the bottom. This crontab job runs a backup script every day at 02:30:

30 2 * * * /home/alice/bin/backup.sh >> /home/alice/backup.log 2>&1

Save and quit. You should see a message like crontab: installing new crontab. That is all: the cron daemon checks for changed crontabs every minute, so the job is live without a restart.

Use full paths. Cron runs jobs with a minimal environment and a short PATH. Write /usr/bin/python3 rather than python3, and redirect output to a log file so you can see what happened.

How to read a cron schedule

A cron schedule is the five fields before the command. Each one narrows down when the job fires.

FieldAllowed valuesExampleMeaning of example
Minute0-5930at minute 30
Hour0-232at 2 AM
Day of month1-31*any day
Month1-12 or jan-dec*any month
Day of week0-7 or sun-sat (0 and 7 are Sunday)1-5Monday to Friday

Four operators cover almost everything: * means any value, a comma builds a list (1,15), a hyphen builds a range (9-17) and a slash sets a step (*/10 is every 10 units). A few common schedules:

*/5 * * * *     /usr/local/bin/poll.sh        # every 5 minutes
0 9 * * 1-5     /usr/local/bin/report.sh      # weekdays at 9:00
0 0 1 * *       /usr/local/bin/invoice.sh     # 1st of every month at midnight
0 */6 * * *     /usr/local/bin/sync.sh        # every 6 hours

Times follow the server's system time zone, not yours. If the server runs in UTC, 0 9 is 9:00 UTC.

Watch the % sign. In a crontab line, % is turned into a newline. Escape it as \%, for example in date +\%F.

How to list, edit and remove cron jobs

The same crontab command manages everything, with one flag per action:

CommandWhat it does
crontab -lLists your installed crontab
crontab -eOpens it in your editor to add, change or delete lines
crontab -rRemoves your entire crontab
crontab -i -rAsks for confirmation before removing
crontab file.txtReplaces your crontab with the contents of a file

Why crontab -r is dangerous

The e and r keys sit next to each other. Type crontab -r by mistake and every job is gone at once, with no prompt and no undo. Two habits protect you:

# keep a backup before editing
crontab -l > ~/crontab.backup

# restore it if something goes wrong
crontab ~/crontab.backup

You can also add alias crontab='crontab -i' to your shell profile so removal always asks first, on versions of cron that support -i.

How to remove a single crontab job

Run crontab -e, delete that line and save. To do it from a script, filter the line out and pipe the rest back in:

crontab -l | grep -v 'backup.sh' | crontab -

Check the result with crontab -l. To pause a job instead, put # at the start of the line.

How to edit another user's crontab

Root can manage any user's crontab with -u:

sudo crontab -u www-data -l
sudo crontab -u www-data -e

The job then runs as that user, with that user's permissions. Note that sudo crontab -e without -u edits root's crontab, not yours. That is a common reason a job seems to vanish: it was saved to a different user. Access can also be restricted with /etc/cron.allow and /etc/cron.deny.

What is the difference between /etc/crontab and user crontabs?

Besides personal crontabs, Linux has system-wide ones: /etc/crontab and any file in /etc/cron.d/. You edit these directly as root, and they need a sixth field: the user to run the command as, placed between the schedule and the command.

# /etc/cron.d/cleanup
# m  h  dom mon dow  user      command
15   3  *   *   *    www-data  /usr/local/bin/cleanup-cache.sh

Packages drop their own jobs into /etc/cron.d/, which makes it the right place for jobs that belong to the machine rather than to one person. On Debian and Ubuntu, file names there must use only letters, digits, underscores and hyphens: a file called cleanup.cron is silently ignored.

Do not mix the formats. A user field inside crontab -e is read as part of the command, and a missing user field in /etc/cron.d breaks the line.

How to use @reboot and other cron shortcuts

Instead of five fields, you can start a line with a special string:

ShortcutSame asRuns
@rebootnoneonce when the cron daemon starts after boot
@yearly or @annually0 0 1 1 *midnight on 1 January
@monthly0 0 1 * *midnight on the 1st
@weekly0 0 * * 0midnight on Sunday
@daily or @midnight0 0 * * *every midnight
@hourly0 * * * *at the start of every hour
@reboot sleep 30 && /home/alice/bin/start-worker.sh

The short sleep gives networking and mounts time to come up, because @reboot fires as soon as cron starts. For services that depend on other services, a systemd timer or unit gives you finer control over ordering.

What are cron.hourly, cron.daily and cron.weekly?

Most distributions ship four folders: /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly. Drop an executable script in one and it runs at that interval, with no schedule line to write. The system runs each folder with run-parts, triggered from /etc/crontab or, on many systems, by anacron, which also catches up on runs missed while the machine was off.

sudo cp rotate-reports /etc/cron.daily/
sudo chmod +x /etc/cron.daily/rotate-reports

# show which scripts run-parts would execute
run-parts --test /etc/cron.daily

Two rules trip people up. The script must be executable, and on Debian and Ubuntu its name must not contain a dot, so rotate-reports.sh is skipped. The --test flag above lists exactly which files will run. You do not choose the time of day either: it comes from the system configuration.

How to check a cron schedule before saving

A wrong field rarely throws an error. The job simply runs at a time you did not expect, or never. Before you paste a line into crontab -e, build or paste the expression into the cron builder. It explains the five fields in plain English and lists the next 10 run times, so you can see that 0 9 * * 1-5 really skips the weekend.

Keep in mind that the builder shows runs in your browser's time zone, while the server uses its own. If they differ, convert the times with the time zone converter. The builder works with five-field expressions, so check @ shortcuts against the table above. If a job is saved but still does not fire, see why a cron job is not running.

For the full syntax, the crontab(5) manual page describes the file format and the crontab(1) manual page covers the command and its flags.

Cron schedule builder showing a five-field expression and a list of upcoming run times
Cron Expression Builder

Not sure when your cron job will actually run?

Set the five fields or paste an existing expression, read it in plain English and see the next 10 runs before you save it to your crontab.

Check my scheduleRuns in your browser

Frequently asked questions

Run crontab -e as the user who should own the job. Your editor opens; add a line with five time fields and a command, such as 0 6 * * * /path/to/script.sh, then save and quit. Cron installs the file and picks it up within a minute, with no restart needed.

User crontabs live in a spool directory: /var/spool/cron/crontabs/ on Debian and Ubuntu, /var/spool/cron/ on Red Hat based systems, one file per user. Do not edit them directly; use crontab -e. System jobs are in /etc/crontab, /etc/cron.d/ and the cron.hourly, daily, weekly and monthly folders.

crontab -e edits a personal crontab whose jobs always run as its owner, so lines have five time fields and a command. /etc/crontab is the system-wide file edited directly by root, and each line adds a user field between the schedule and the command to say who the job runs as.

Open crontab -e and start the line with @reboot instead of five time fields, for example @reboot /home/alice/bin/start.sh. It runs once when the cron daemon starts. Add a short sleep first if the script needs networking or mounted disks that may not be ready yet.

Run crontab -e, delete or comment out that one line, and save. From a script, use crontab -l | grep -v 'pattern' | crontab - to reinstall everything except matching lines. Avoid crontab -r, which removes every job at once without asking.