Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Using crontab

August 06, 2023 — Gregg Szumowski

Crontab is a utility found on Linux and UNIX that allows you to schedule repetitive tasks on your system, like backups, updates, etc.

The crontab command manages a table containing specially formatted lines of text which is processed periodically by the system. Each line consists of a “schedule” followed by one or more commands to be processed at the scheduled time.

If you want to use the vim, emacs or nano editor for editing cron jobs, then, set the following in your environment and in your ~/.bash_profile file:

EDITOR=vim

Then, to edit the default cron job, as root enter this:

# crontab -e

If this is the first time you are editing the crontab, you may want to put in the following header as a reminder or a template of the format:

#MINUTE(0-59) HOUR(0-23) DAYOFMONTH(1-31) MONTHOFYEAR(1-12) DAYOFWEEK(0-6)
#Note that 0=Sun and 7=Sun
#14,15 10 * * 0 /usr/bin/somecommmand >/dev/null 2>&1

The sample “commented out command” will run at 10:14 and 10:15 every Sunday. There will be no mail sent to the user because of the >/dev/null 2>&1 entry.

To list all cron jobs enter:

# crontab -l

If you’re logged in as root you can view or edit the crontab entries of any of the users:

# crontab -l -u <username>
# crontab -e -u <username>

You can also run jobs on an hourly, daily, weekly or monthly basis by adding shell scripts into one or more of the following directories:

  • /etc/cron.hourly/
  • /etc/cron.daily/
  • /etc/cron.weekly/
  • /etc/cron.monthly/

You can find out more information on the crontab manpage.

Tags: cli, crontab, motd