crontab
Overview
crontab installs, lists, and removes per-user crontab files that the cron daemon executes. Prefer crontab -e over editing spool files by hand. System-wide jobs live under /etc/cron.d and friends — see the cron chapter.
Syntax
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i]Common Options
| Option | Description |
|---|---|
-l |
List current crontab |
-e |
Edit current crontab in $EDITOR / $VISUAL |
-r |
Remove entire crontab |
-i |
Prompt before -r |
-u user |
Operate on another user’s crontab (root) |
Key Use Cases
- Schedule periodic tasks as your own user
- Install a crontab from a file in config management
- Audit what a user has scheduled
- Clean up obsolete jobs safely
Safety
crontab -rdeletes all of that user’s jobs without a backup — use-ior list first.
- Installing from a file replaces the whole crontab; keep the source in git.
- Root using
-uaffects another account’s automation — verify the user name.
Examples with Explanations
List and edit
crontab -l
EDITOR=vim crontab -eEmpty crontab may print no crontab for user (exit status can be non-zero depending on version).
Install from a file (idempotent in config mgmt)
cat > /tmp/my.cron <<'EOF'
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
*/15 * * * * /usr/local/bin/poll.sh >>$HOME/logs/poll.log 2>&1
0 3 * * * /usr/local/bin/backup.sh >>$HOME/logs/backup.log 2>&1
EOF
crontab /tmp/my.cron
crontab -lRedirects and absolute paths avoid “worked in SSH, failed in cron”.
Append a line carefully
(crontab -l 2>/dev/null; echo '0 4 * * 0 /usr/local/bin/weekly.sh') | crontab -Race-prone if two installers run at once — prefer full-file installs from CM.
Remove with confirmation
crontab -l > ~/crontab.backup.$(date +%F)
crontab -i -rAlways backup before delete.
Root manages another user
sudo crontab -u deploy -l
sudo crontab -u deploy -eValidate schedule fragments
# After editing, re-list and reason about times:
crontab -l
# Cross-check system timers too:
systemctl list-timers --allCommon job examples
# every 15 minutes
*/15 * * * * /usr/bin/flock -n /tmp/poll.lock /usr/local/bin/poll.sh
# weekdays 09:00
0 9 * * 1-5 /usr/local/bin/report.sh
# after reboot
@reboot /usr/local/bin/start-tunnel.sh
See cron for field semantics, environment, and debugging.
Understanding Output
crontab -l prints the file body (comments and environment assignments included). Editors invoked by -e must exit zero for the new file to be installed; syntax is checked lightly — a bad command still “installs”.
Notes & Pitfalls
- Per-user crontabs do not include a username field (unlike
/etc/crontab).
%is special in some cron implementations — escape as\%in commands.
- SELinux may block scripts that work in an interactive shell.
- Desktop users: session-dependent jobs often need systemd user timers + lingering, not cron.
crontab -eusesselect-editoron first run (Debian/Ubuntu).
Additional Resources
man 1 crontab
man 5 crontab
- crontab.guru