Tutorial

How to edit a crontab.

Run crontab -e, change the lines, save and quit. The new crontab is installed the moment the editor closes, with no restart. The rest of this page covers the parts that trip people up: which editor opens, whose crontab you are editing, and how to remove one job without wiping the lot.

Edit your own crontab

crontab -e

This opens a copy of your crontab (an empty one if you have none yet). When you save and quit, crontab checks the file and installs it. Debian's manual puts it simply: after you exit the editor, the modified crontab is installed automatically. You do not need to restart cron.

If a line has a mistake, crontab refuses to install it and offers to reopen the editor: see errors in crontab file, can't install. To check a crontab before you install it, paste it into the crontab checker.

Choose the editor

crontab -e uses the editor named in the VISUAL or EDITOR environment variable. If neither is set, POSIX says the default is vi; Debian and Ubuntu use /usr/bin/editor instead, and the first time you run it they ask you to pick one.

EDITOR=nano crontab -e                 # just this once
echo 'export EDITOR=nano' >> ~/.bashrc  # from now on (new shells)
select-editor                          # Debian/Ubuntu: change the saved choice

Stuck in vi? Press Esc, type :wq and press Enter to save and quit, or :q! to quit without saving.

Edit root's or another user's crontab

sudo crontab -e              # root's crontab, NOT yours
sudo crontab -e -u www-data  # another user's crontab
sudo crontab -l -u www-data  # list it

Jobs run as the owner of the crontab. A job added with sudo crontab -e runs as root, which is a common reason a script suddenly writes root-owned files. Edit your own crontab without sudo. If you switched user with su, add -u with the user name: the Debian manual warns that su can confuse crontab about whose file it is editing.

The system crontab is different

/etc/crontab and the files in /etc/cron.d/ are not edited with crontab -e. Open them with an editor as root, and remember they have an extra user field before the command:

sudo nano /etc/cron.d/backup
# m  h  dom mon dow user   command
30   2  *   *   *   alice  /home/alice/backup.sh

More on the two formats: crontab vs /etc/crontab. Never edit the spool files under /var/spool/cron directly; changes there may not be picked up.

Comments, and turning a job off

A line whose first non-space character is # is a comment. Comments must be on their own line: cron passes everything after the time fields, a trailing # included, to the shell as the command. The shell usually ignores it, but a % inside it still becomes a newline, and on a variable line such as MAILTO=me@example.com # note the comment ends up in the value.

# Nightly backup (comment on its own line: fine)
30 2 * * * /home/alice/backup.sh

# Disabled for now: put # in front to turn a job off
# 0 * * * * /home/alice/sync.sh

0 3 * * * /home/alice/report.sh   # avoid: passed to the shell as part of the command

Remove one job (and why not crontab -r)

To remove a single job, open crontab -e and delete its line. In a script, filter it out and install the rest:

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

crontab -r deletes your whole crontab, at once and without asking. On a keyboard r sits next to e, so a slip on crontab -e can wipe every job. On Debian and on cronie (Fedora, RHEL) crontab -i -r asks for confirmation first. Keep a backup:

crontab -l > ~/crontab.bak    # back up
crontab ~/crontab.bak         # restore (replaces the whole crontab)

Install or update from a file

crontab FILE replaces your entire crontab with the contents of FILE, and crontab - reads it from standard input. That makes it easy to keep a crontab in version control, or to add a line from a script without opening an editor:

(crontab -l 2>/dev/null; echo '0 3 * * * /home/alice/report.sh') | crontab -

Run that twice and you get the line twice; check with crontab -l. With Ansible, the cron module tags each entry with a #Ansible: name marker line and finds it again by that name, so give every entry a unique name:

- name: Nightly backup
  ansible.builtin.cron:
    name: nightly backup
    minute: "30"
    hour: "2"
    job: /home/alice/backup.sh

Check what you saved

crontab -l

Then confirm the schedule means what you think in the cron explainer, and see how to list every cron job on the server. If the job doesn't run, start with where are cron logs.

On a Mac

crontab -e works the same on macOS and opens vi unless EDITOR is set. If saving fails with "Operation not permitted", see this fix; more in crontab on Mac.

Sources

Checked against these official manuals. Last verified 19 November 2026.