How to edit crontab
To edit your crontab, run crontab -e. It opens your cron jobs in a text editor; add or change a line, save and quit, and crontab checks the syntax and installs it. Cron picks up the change by itself, with no restart. crontab -l lists the jobs and crontab -r deletes them all.
Five things to know before you edit
Your shell's EDITOR decides the editor
crontab -eopens$VISUAL, else$EDITOR. RunEDITOR=nano crontab -eto skip vi.Every user has a separate crontab
sudo crontab -eedits root's, not yours. Use-uto pick the user the job should run as.Don't edit the file in /var/spool/cron
That's crontab's private copy. Edit through the crontab command, which checks the syntax and tells cron to reload.crontab -r deletes without asking
Andrsits next toeon the keyboard. Keep a backup withcrontab -l > crontab.bak.- Cron notices the changed crontab within a minute. A job you add now runs at its next scheduled time.
Open, edit and save
Run crontab -e as the user the jobs should run as. Each line is five schedule fields and a command, such as */5 * * * * /usr/local/bin/backup.sh to run a script every 5 minutes. Lines starting with # are comments, so you can switch a job off by putting # in front of it.
In nano, save with Ctrl+O and Enter, then quit with Ctrl+X. In vi or vim, press i to start typing, Esc to stop, then type :wq and Enter. When you quit, crontab prints crontab: installing new crontab. If you saved without a change it says No modification made, and if a line is wrong it refuses to install and asks Do you want to retry the same edit?
Cron checks for changed crontabs every minute, so there's nothing to restart or reload. A job is read in the server's time zone and starts at its next matching minute.
Choose the editor
crontab opens the editor named in VISUAL, or EDITORif that's unset. With neither, Debian and Ubuntu run sensible-editor, which asks you to pick one the first time and remembers the answer in ~/.selected_editor; run select-editor to change it. Fedora, RHEL and macOS fall back to vi.
# Use nano for this one edit
EDITOR=nano crontab -e
# Make it permanent: add to ~/.bashrc (or ~/.zshrc)
export VISUAL=nano
export EDITOR=nanoWith sudo, your own variables may not reach crontab. Pass the editor through explicitly: sudo EDITOR=nano crontab -e.
List cron jobs
crontab -l prints your crontab. With none it prints no crontab for alice and exits with status 1. It shows only your own crontab: jobs in /etc/crontab and /etc/cron.d/, and the scripts in /etc/cron.daily and its siblings, are system jobs, so read those files directly.
To list every user's crontab, loop over the accounts as root:
for u in $(cut -d: -f1 /etc/passwd); do
sudo crontab -u "$u" -l 2>/dev/null | sed "s/^/$u: /"
doneEdit another user's crontab, or root's
A job runs as the user who owns the crontab. sudo crontab -eedits root's crontab, so jobs there run as root and create files owned by root. To run a job as a service account, edit that account's crontab: sudo crontab -u www-data -e, and -l or -r in place of -e to list or delete it. Without root, -u fails with must be privileged to use -u. In /etc/crontab and /etc/cron.d/ the user is a sixth field before the command instead, as shown on the @reboot page.
Edit crontab from a script
crontab file installs a file as your whole crontab, and crontab - reads it from standard input. Either replaces everything, so to add one job, print the current crontab and the new line together:
# Add a job without opening an editor
(crontab -l 2>/dev/null; echo "*/5 * * * * /usr/local/bin/backup.sh") | crontab -The 2>/dev/nullhides “no crontab” when the user has none yet. Running it twice adds the job twice. For bigger changes, dump the crontab to a file, edit it and install it; on Debian and Ubuntu, crontab -n checks a file without installing it:
crontab -l > crontab.txt # dump the current crontab
nano crontab.txt # change it
crontab -n crontab.txt # Debian and Ubuntu: check the syntax only
crontab crontab.txt # install it, replacing the old oneRemove a job, or the whole crontab
To remove one job, open crontab -e and delete its line, or comment it out. crontab -r deletes the entire crontab at once and doesn't ask. crontab -i -r asks really delete alice's crontab? (y/n) first. If you deleted one by mistake and have no backup, root can sometimes recover the lines from the cron log, which records the commands it ran but not their schedules.
Where crontabs are stored
Debian and Ubuntu keep each user's crontab in /var/spool/cron/crontabs/; Fedora and RHEL in /var/spool/cron/. The files start with # DO NOT EDIT THIS FILE - edit the master and reinstall. Editing them in place skips the syntax check and may not make cron reload, so use crontab -e.
crontab error messages
| Message | What it means |
|---|---|
no crontab for alice | The user has no crontab yet. crontab -e creates one. |
bad minute
errors in crontab file, can't install. | A field is out of range or mistyped (here a minute of 61); the first line names the field. After crontab -e, answer y to "Do you want to retry the same edit?" to fix it. |
new crontab file is missing newline before EOF, can't install. | The last line has no line break. Add one; this happens when you pipe a file in with printf or echo -n. |
must be privileged to use -u | Only root can open another user's crontab. Prefix the command with sudo. |
You (alice) are not allowed to use this program (crontab) | The user isn't listed in /etc/cron.allow, or is listed in /etc/cron.deny. |
No modification made | You saved without changing anything, so nothing was installed. |
Check the schedule before you save