Managing a crontab file without surprises
Knowing the schedule syntax is half the job. The other half is the crontab file itself: how to open it safely, what environment your command really gets, and where its output goes. Most “cron does not work” problems live in that second half.
The commands that touch your crontab
Each user account has its own table of scheduled jobs. You never edit the stored file directly; you go through the crontab program, which checks the syntax before it saves.
| Command | What it does |
|---|---|
crontab -e | Opens your table in the editor named by $EDITOR or $VISUAL |
crontab -l | Prints your current table |
crontab -r | Deletes your whole table with no undo |
crontab file.txt | Replaces your table with the contents of a file |
sudo crontab -u www-data -l | Shows another user’s table (needs root) |
Because -r sits next to -e on the keyboard, make a backup habit: crontab -l > ~/crontab.bak before big edits. You can restore it later with crontab ~/crontab.bak.
Anatomy of a working entry
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
# m h dom mon dow command
15 3 * * * /home/ana/bin/backup.sh >> /home/ana/logs/backup.log 2>&1
The lines at the top are variable assignments that apply to every job below them. Comments start with #. The entry runs a backup script at 03:15 each day and appends everything it prints, errors included, to a log file.
Cron gives you a bare environment
A job does not start from your interactive shell. It does not read ~/.bashrc, it does not get your aliases, and its PATH is usually just a couple of system folders. A command that works in your terminal can fail under cron with “command not found”. Three fixes, from most to least robust:
- Use full paths for every program, for example
/usr/bin/python3instead ofpython3. Find them withcommand -v python3. - Set
PATHat the top of the crontab, as in the example. - Put the logic in a script that sets up its own environment, and call only that script from cron.
The working directory is normally your home folder, so relative file paths inside a script point somewhere you may not expect. Start scripts with an explicit cd to the folder they need.
Where the output goes
Anything a job writes to standard output or standard error is collected and, on many systems, emailed to the account owner. If no mail system is installed, that output is simply lost, along with the error that would have explained the failure. Decide on purpose:
>> file.log 2>&1keeps everything in a log you can read later.> /dev/null 2>&1throws everything away. Use it only for jobs you already trust.MAILTO="you@example.com"sends the output by mail when a mail transfer agent is set up;MAILTO=""turns mail off.
The order of the redirections matters. 2>&1 >> file sends errors to the old standard output, not to the file.
Debugging a job that never runs
- Run
crontab -land confirm the entry is really saved under the user you think. - Check that the cron service runs:
systemctl status cronon Debian and Ubuntu,systemctl status crondon Red Hat style systems. - Look at the system log:
grep CRON /var/log/syslog, orjournalctl -u cron. A line there proves cron started the command. - Make sure the script is executable (
chmod +x) and has a shebang line such as#!/bin/bash. - Escape percent signs. In a crontab line, an unescaped
%becomes a newline, sodate +%Fmust be writtendate +\%F. - Reproduce the bare environment:
env -i /bin/sh -c '/home/ana/bin/backup.sh'shows errors that only appear under cron.
User crontab or system crontab?
Files in /etc/crontab and /etc/cron.d/ use the same schedule but add a sixth column, the user name, before the command. Your personal table from crontab -e has no user column. Mixing the two formats is a common reason a job silently tries to run a program called root.
Questions
Do I need to restart cron after editing?
No. The cron daemon notices changes saved through crontab -e on its own.
How do I stop two copies of a slow job overlapping?
Wrap the command in a lock, for example flock -n /tmp/backup.lock /home/ana/bin/backup.sh. A second copy exits at once if the first still holds the lock.
Is cron available on a laptop that sleeps?
Plain cron skips jobs whose time passed while the machine was off. Tools such as anacron or systemd timers with the persistent option catch up after boot.