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 EDITOR if 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 -e edits 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/null hides “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.
Edit crontab on a Mac
macOS still ships cron, and crontab -e, -l and -r work as on Linux. The editor is vi unless you set EDITOR, and there's nothing to install or start: launchd starts cron by itself once a crontab exists. Crontabs are stored in /usr/lib/cron/tabs/ (a link to /private/var/at/tabs/), readable only by root.
# vi is the default editor on macOS; use nano for this one edit
EDITOR=nano crontab -e
# Homebrew tools aren't on cron's PATH, so set it at the top
PATH=/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin
# Keep the output in a file instead of local mail
30 2 * * * /Users/alice/bin/backup.sh >> /Users/alice/Library/Logs/backup.log 2>&1Most cron jobs that fail on a Mac fail for one of these reasons:
| What you see | Why | Fix |
|---|---|---|
| Operation not permitted | Since macOS 10.15 Catalina, privacy protection blocks cron from Desktop, Documents, Downloads, iCloud Drive and external disks. | System Settings › Privacy & Security › Full Disk Access, click +, press Cmd+Shift+G, enter /usr/sbin/cron and switch it on. Or keep the job's files outside those folders. |
| Missed runs while the Mac was asleep | cron skips any run that falls while the Mac sleeps and doesn't catch up when it wakes. | Use launchd (below), which runs a missed job once on wake, or wake the Mac first with sudo pmset repeat wakeorpoweron MTWRFSU 02:25:00. |
| command not found for brew, node or python3 | cron's PATH is /usr/bin:/bin, without Homebrew's /opt/homebrew/bin (Apple silicon) or /usr/local/bin (Intel). | Set PATH= at the top of the crontab, or use full paths from which. |
| “You have mail” in Terminal | Cron mails each job's output to your local mailbox, /var/mail/alice. There's no cron log file. | Read it with mail, then redirect the output to a file and add MAILTO="". |
| crontab: temp file must be edited in place | The editor saved by writing a new file and renaming it, which macOS's crontab refuses. | Use nano or vi, or in vim :set backupcopy=yes. |
Apple recommends launchd over cron for new jobs. A job is a property list in ~/Library/LaunchAgents; StartCalendarInterval takes the same fields as cron (Minute, Hour, Day, Weekday, Month), and a field you leave out matches every value, like *. Unlike cron, launchd runs a job that was due while the Mac slept once it wakes, so a laptop doesn't silently skip it. This one runs a script at 02:30 every day:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.alice.backup</string>
<key>ProgramArguments</key>
<array>
<string>/Users/alice/bin/backup.sh</string>
</array>
<!-- Same as 30 2 * * *, but runs on wake if the Mac slept through 02:30 -->
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>2</integer>
<key>Minute</key>
<integer>30</integer>
</dict>
<key>StandardOutPath</key>
<string>/Users/alice/Library/Logs/backup.log</string>
<key>StandardErrorPath</key>
<string>/Users/alice/Library/Logs/backup.log</string>
</dict>
</plist>Check the file, then load it. An agent in ~/Library/LaunchAgents also loads by itself at each login. To change it, unload it with bootout, edit and load it again.
plutil -lint ~/Library/LaunchAgents/com.alice.backup.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.alice.backup.plist
launchctl kickstart gui/$(id -u)/com.alice.backup # run it once now
launchctl bootout gui/$(id -u)/com.alice.backup # remove itInstall cron and check that it's running
Most Linux servers ship with cron running, but minimal images and containers often have no crontab command at all. Debian and Ubuntu call the package and the systemd service cron; Fedora and RHEL install cronie, whose service is crond. enable --now starts it and keeps it running after a reboot.
# Debian and Ubuntu: package and service are both "cron"
sudo apt install cron
systemctl status cron
sudo systemctl enable --now cron
# Fedora, RHEL, Rocky, Alma: package "cronie", service "crond"
sudo dnf install cronie
systemctl status crond
sudo systemctl enable --now crondTo see whether a job ran, read cron's log. It records each run as a CMD line with the user and the command, but not the command's output; redirect that to a file in the crontab line.
journalctl -u cron --since today # Debian and Ubuntu
grep CRON /var/log/syslog # Ubuntu with rsyslog
journalctl -u crond --since today # Fedora and RHEL
sudo tail /var/log/cron # RHEL with rsyslogCron job not running? Check these in order
Start with the log from the section above. If it has no CMD line for your job at the minute it should have run, cron never started it: the problem is the service, the crontab or the schedule. If the line is there, cron did its part and the command failed, almost always because cron runs it with a much smaller environment than your terminal.
| What you see | Why | Fix |
|---|---|---|
| No CMD line in the log at all | Cron isn't running, or the job isn't installed for the user you think | systemctl status cron (crond on Fedora and RHEL), then crontab -l as the user, or sudo crontab -l for root. |
| Runs, but at the wrong time | The schedule is read in the server's time zone, or the fields are in the wrong order | Check timedatectl and paste the five fields into the explainer; minute comes first. |
| command not found in the output | Cron's PATH is only /usr/bin:/bin, not your login shell's | Use full paths (which node shows them), or set PATH= at the top of the crontab. |
| Works in your terminal, fails from cron | Cron runs /bin/sh without your .bashrc or .profile, so aliases and exported variables are missing | Set the variables in the crontab or the script, add SHELL=/bin/bash for bash syntax, and start the script with a #! line. |
| Can't find its files | The job starts in the user's home directory, not the script's | cd /path/to/app && before the command, or absolute paths in the script. |
| Command is cut off at a date | An unescaped % ends the command; the rest becomes standard input | Write \%, as in date +\%F. |
| Permission denied | The script isn't executable, or the crontab's user can't read what it touches | chmod +x script.sh, or put the job in the crontab of the user who owns the files. |
| (CRON) info (No MTA installed, discarding output) | Cron tried to mail the job's output and there's no mail server, so errors vanish | Redirect the output: >> /var/log/job.log 2>&1, and MAILTO="" to stop the mail attempts. |
| A file in /etc/cron.d or /etc/cron.daily never runs | Debian and Ubuntu skip file names containing a dot, such as backup.sh | Rename it to backup. Files in /etc/cron.d must also be owned by root, not writable by group or others, and have the user field. |
To reproduce a failure, run the job the way cron does. Add a temporary line that saves cron's environment, wait a minute, delete the line, then run your command with only those variables:
# 1. Capture the environment cron gives your jobs (remove the line afterwards)
* * * * * env > /tmp/cron-env.txt
# 2. Run the job by hand with only that environment
env -i $(cat /tmp/cron-env.txt) /bin/sh -c '/usr/local/bin/backup.sh'A crontab that avoids most of these problems sets its shell and path, starts in a known directory and keeps the output:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
# Full paths, a working directory, and the output kept in a log
*/5 * * * * cd /srv/app && /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1crontab error messages
| Message | What it means |
|---|---|
crontab: command not found | Cron isn't installed, which is common in containers and minimal images. Install the cron package (cronie on Fedora and RHEL). |
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