Mastering crontab: Common Pitfalls and How to Fix Them
This guide explains what crontab does, its syntax and options, provides practical examples, highlights typical problems such as environment variables, time misconfiguration, special characters and output redirection, and offers debugging tips and tools for reliable Linux task scheduling.
What crontab can do
crond is the Linux daemon that executes scheduled tasks, similar to Windows Task Scheduler. After installation it runs automatically, checking every minute for jobs to run. Linux task scheduling is divided into system tasks (e.g., cache flushing, log cleanup) and user tasks (e.g., backups, email reminders) stored in /var/spool/cron per user.
Typical uses
Common applications include periodic data backup, system health checks, data collection, configuration updates, and report generation.
crontab usage format
crontab [-u user] [file]
crontab [-u user] [-e|-l|-r|-i]Options: -u user – specify the user whose crontab is edited (usually run by root). file – file containing a list of cron jobs; if omitted, commands are read from standard input. -e – edit the crontab. -l – list the crontab. -r – remove the crontab. -i – ask for confirmation before removal.
crontab file syntax
minute hour day month week commandminute – 0‑59 hour – 0‑23 day – 1‑31 month – 1‑12 week – 0‑7 (0 or 7 = Sunday) command – any executable command or script
Special characters can be used in any field:
* – all possible values , – list separator (e.g., 1,2,5 ) - – range (e.g., 2-6 ) / – step value (e.g., 0-23/2 for every two hours, */10 for every ten minutes)
Example cron jobs
0 /3 /usr/local/apache2/apachectl restartRestart Apache every three hours. 30 3 6 /webdata/bin/backup.sh Run a backup script at 03:30 on every Saturday. 0 0 1,20 fsck /dev/sdb8 Check the disk on the 1st and 20th of each month.
10 5 */5 * echo "">/usr/local/apache2/log/access_logClean Apache access logs on the 5th,10th,15th,20th,25th,30th of each month at 05:10.
System‑wide crontab (/etc/crontab)
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthlyThe first four lines set environment variables; the remaining lines define scheduled tasks.
Debugging tool
An online parser (https://crontab.guru) can show the exact execution times for a given expression.
Common pitfalls
1. Environment variables
Commands should use absolute paths because the PATH in a cron job is often limited (e.g., /usr/bin:/bin). You can source your profile before the command:
* * * * * source $HOME/.bash_profile && command2. Time configuration errors
Writing * 2 * * * runs the job every minute during the 2 AM hour, not once at 02:00. The correct expression is 0 2 * * *.
3. Special character %
In crontab, the first % starts standard input; subsequent % are newline characters. Escape them with a backslash when needed.
* * * * * cat >> /tmp/file 2>&1 \% escaped percent4. Output redirection
Without redirection, a job’s output is mailed to the user. Set MAILTO="" or redirect output to /dev/null to avoid mail and disk‑space issues:
MAILTO=""
0 * * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1Debugging workflow
Check /var/log/cron to see if the job ran.
If not, verify the schedule, environment variables, special characters, and permissions.
Ensure the crond service is running ( systemctl status crond.service on CentOS 7+ or service crond status on older versions).
Redirect command output to a log file to capture errors, e.g.,
* * * * * python /path/script.py >> /tmp/script.log 2>&1.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
