
Where crontab files live
Use the following locations depending on whether you need per-user jobs, periodic scripts, or system-wide jobs:Managing user crontabs with the crontab command
Use thecrontab helper to edit, list, or remove a user’s crontab:
crontab -e— edit the current user’s crontab (opens the editor defined by$EDITOR).crontab -l— list the current user’s crontab.crontab -r— remove the current user’s crontab.crontab -r -u username— remove another user’s crontab (requires root).
Crontab format: schedule fields and command
Each non-comment line in a crontab defines one job. The first five whitespace-separated fields set when the job runs:- In a per-user crontab, the sixth field onward is the command to execute.
- In a system crontab (such as
/etc/crontabor files under/etc/cron.d/), the sixth field is the user account to run the command as, and the rest form the command.
/etc/cron.d file (fields: minute hour day month weekday user command):
Special characters and common patterns
*— every unit (every minute, every hour, etc.).-— range (e.g.,9-17for hours 9 through 17)./— step values (e.g.,*/10for every 10 minutes).,— list values (e.g.,11,12for minute 11 and 12).- You can combine ranges, steps, and lists in one field.
Using run-parts for periodic directories
Many Linux distributions userun-parts to execute scripts placed in periodic directories:
/etc/cron.hourly/etc/cron.daily/etc/cron.weekly/etc/cron.monthly
chmod +x script).
Practical demo: create several user cron jobs
Below is a consolidated example showing how a user (student) might add recurring jobs withcrontab -e. These examples demonstrate specific minutes/hours, intervals, ranges, and lists.
Open the crontab for the student user:
:wq). The new crontab is installed automatically.
Verify the installed crontab:
You can combine ranges, intervals, and lists in a single crontab entry. Always test commands manually to verify paths and behavior before scheduling them in cron.
System-wide cron jobs
For system-level scheduled tasks, prefer adding files to/etc/cron.d/ rather than editing /etc/crontab directly. Files placed in /etc/cron.d/ must use the system crontab format (i.e., include the user field).
Example: create /etc/cron.d/sysjob that runs every 2 minutes as root.
/etc/cron.d/sysjob:
Removing a user’s crontab (as root)
To remove a specific user’s crontab as root:Checking the cron daemon (crond)
On systemd-based systems, check the status of the cron daemon (name varies by distribution:crond or cron):
- Confirm the cron service is active and enabled.
- Inspect logs for cron-related activity (e.g.,
journalctl -u crondor/var/log/cron). - Make sure scripts in periodic directories are executable and have correct shebangs.
- Use full paths in crontab commands and redirect stdout/stderr to files for easier debugging.
Wrap-up
You now know how to:- Write crontab schedule expressions and the meaning of each time field.
- Manage per-user crontabs with
crontab -e,-l, and-r. - Use
run-partsfor periodic scripts in/etc/cron.*. - Create system-wide cron jobs under
/etc/cron.d/or/etc/crontab. - Verify the cron daemon and troubleshoot scheduled jobs.