Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

crontab

Edit, list or remove the cron job list (crontab) of a user.

Level
RHCSA exam
Group
Scheduling
Needs root
For changes
Package
cronie

What it does

Each user can have a crontab: lines with 5 time fields (minute, hour, day of month, month, day of week) and a command. crontab -e opens it in your editor and installs it when you save; crond checks the lists every minute.

root can manage the crontab of any user with -u. The files are stored in /var/spool/cron/USER — do not edit them directly. Who may use crontab is set in /etc/cron.allow and /etc/cron.deny.

How to write it

crontab [-u USER] FILE
crontab [-u USER] -e | -l | -r [-i]
crontab -T FILE

Main parts

MINUTE
0–59.
HOUR
0–23.
DAY-OF-MONTH
1–31.
MONTH
1–12 or jan–dec.
DAY-OF-WEEK
0–7 (0 and 7 = Sunday) or sun–sat.
* , - /
Any value, list (1,15), range (1-5), step (*/10).
@reboot @hourly @daily @weekly @monthly @yearly
Shortcuts instead of the 5 fields.

Options 11

Most used

-e
Edit the crontab (creates it if needed).
-l
List the crontab.
-r
Remove the whole crontab.
-i
Ask before removing (with -r).
-u USER
Work on the crontab of another user (root).
FILE
Install this file as the new crontab (- = standard input).
-T FILE
Test the syntax of a crontab file (newer cronie).
-s
Add the SELinux security context (MLS systems).
-n HOST
Set the host that runs the jobs (cluster mode).
-c
Show the cluster host.
-V
Show the version.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Edit your crontab

    crontab -e

    Opens vi (or $EDITOR). Save and quit to install it.

  2. 2List your jobs

    crontab -l
    */10 * * * * /usr/bin/df -h / >> /var/tmp/disk.log

    No crontab: "no crontab for USER".

  3. 3Manage another user's jobs

    crontab -e -u lisa
    crontab -l -u lisa

    The RHCSA way for tasks "user X must run …".

  4. 4Weekdays at 14:23

    crontab -l | grep logger
    23 14 * * 1-5 logger "anna was here"

    Monday to Friday.

  5. 5Install a crontab from a file

    crontab mycron.txt

    Replaces the whole list. Good for versioned crontabs in git.

  6. 6Add one line without an editor (scripts)

    (crontab -l 2>/dev/null; echo '0 2 * * * /usr/local/bin/backup.sh') | crontab -

    Keeps the existing lines.

  7. 7Back up all crontabs

    tar -czf /root/crontabs.tgz /var/spool/cron /etc/cron.d /etc/crontab

    Before you rebuild a server.

  8. 8Remove a crontab with a question

    crontab -ri
    crontab: really delete student's crontab? y

    Without -i, -r removes everything at once.

  9. 9Run a job at every boot

    crontab -l | grep reboot
    @reboot /usr/local/bin/start-tunnel.sh

    A systemd unit is often the better choice.

  10. 10Log the output of a job

    crontab -l
    0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

    Without it, output goes to local mail.

  11. 11Find all cron jobs on a system (security)

    ls -l /var/spool/cron/ /etc/cron.d/; cat /etc/crontab

    Attackers like cron for persistence; check for unknown jobs.

  12. 12Check the cron log

    grep CROND /var/log/cron | tail

    Shows when jobs ran and for which user.

Watch out

  • crontab -r is next to -e on the keyboard and deletes everything without asking. Use -ri.
  • cron uses a small PATH: write full paths.
  • % in a crontab line means a newline; write \% (for example in date +\%F).

Files

/var/spool/cron/USER
The crontabs of users.
/etc/crontab, /etc/cron.d/
System crontabs (with a user field).
/etc/cron.allow, /etc/cron.deny
Who may use crontab.
/var/log/cron
Log of crond.

Exit codes

0 :: Done. 1 :: Error (not allowed, syntax error, no crontab).