The vi editor launches when the crontab -e command is used. There are two modes in this editor: command and insert.

You can enter commands, like saving the file, in command mode. In this mode, keystrokes are translated into commands instead of displaying text on the screen.

By entering the appropriate command, you can get to the insert mode. You can access the insert mode by entering the appropriate command. This is typically the i (insert) or inserts the key. When you enter characters in this mode, the text editor opens right away to display them. Pressing the ESC key will bring up the command mode.

In Linux-based operating systems, Cron is a time-based job scheduler utility that can be used to run commands or scripts at specific times, dates, or intervals.

Automated tasks like backup, cache clearing, disk space monitoring, and many more are very helpful for system administrators.

The crontab command can be used to install, uninstall, create, and edit cron jobs. /var/spool/cron/crontabs is the location of each user’s individual crontab files. The background-running cron job periodically checks the /etc/crontab file and the /etc/cron.*/ directories.

Cron configuration files come in two different varieties:

The System Crontab :

For system services, this crontab is executed as root.

The User Crontab :

Each user has their own crontab file, which is used to schedule user-specific tasks. This crontab runs as a specific user.

We will use examples to demonstrate how to use crontab in this tutorial.

Requirements

  • a Linux-installed computer.
  • a non-root user who has sudo access.

Various Crontab Options

The options that can be used with the crontab command for Crontab are all listed below:

crontab -e :

Using this, you can edit or make a new crontab file.

crontab -u username -e :

For a specific user, the crontab file can be edited or created using this option.

crontab -l :

You can use this option to see what’s in your crontab file.

crontab -r :

Using this option, you can delete your crontab file.

crontab -a filename :

Using this option, a particular file can be installed as a crontab file.

Syntax of Crontab

The crontab file’s basic syntax is displayed below:

* * * * * USERNAME path-of-the-command arg1 arg2

Where:

  1. Minutes (0–59) are specified by the first * operator.
  2. Second * operator: Hours (0-23) are specified.
  3. Third * operator: Day (0-31) is specified.
  4. Fourth * operator: Month (0–12) is specified.
  5. The fifth * operator specifies the weekday (from 0 to 7).

Crontab Basic Commands

With the command listed below, you can edit the root user’s crontab file:

crontab -e

Choose your preferred editor and hit Enter.

The following command will list every cron job file:

ls /etc/cron*

The output should appear as follows:

/etc/crontab

/etc/cron.d:

anacron php5

/etc/cron.daily:

0anacron apport bsdmainutils cracklib-runtime google-chrome man-db passwd update-notifier-common

apache2 apt chrome-remote-desktop dpkg logrotate mlocate popularity-contest upstart

/etc/cron.hourly:

/etc/cron.monthly:

0anacron

/etc/cron.weekly:

0anacron apt-xapian-index fstrim man-db update-notifier-common

With the command below, you can view every crontab job for the user vyom:

crontab -u vyom -l

Run the following command to eliminate all crontab jobs:

crontab -r

With the command below, you can eliminate every crontab job for the user vyom:

crontab -u vyom -r

Run the ntpdate command with the /dev/null output redirection to run it every day at 8:05 AM:

crontab -e

Insert the following phrase:

  • 05 08 * * * root /usr/bin/ntpdate > /dev/null 2>&1
  • Add the next line to make ntpdate run every Monday through Friday at 10:00 AM:
  • 00 10 * * 1,5 root /usr/bin/ntpdate > /dev/null 2>&1
  • Add the following line to run the ntpdate command every day at 8:20, 9:20, and 11:20:
  • 20 08,09,11 * * * root /usr/bin/ntpdate > /dev/null 2>&1
  • Add the following line to the command prompt to run ntpdate on the first and fifteenth of every month:
      • 1,15 * 1 root /usr/bin/ntpdate > /dev/null 2>&1

Use Special String in Crontab

The first five fields in your crontab file can also be replaced by a special string.

The list of all special strings is as follows:

@reboot: This will operate following a system restart.

@hourly: Once every hour, this will run.

@daily: Every day, this will run.

@weekly: This will occur once per week.

@monthly: This will occur once per month.

For instance, add the line below to run the ntpdate command after a system reboot:

@reboot root /usr/bin/ntpdate > /dev/null 2>&1

Add the line @monthly root /usr/bin/ntpdate > /dev/null 2>&1 to make the ntpdate command run each month.

The following command will also create a backup of your root cron jobs:

crontab -l > /root/cron.bak

Run the following command to backup a specific user’s cron jobs:

crontab -u username -l > /username_cron.bak

How to Configure a WordPress Cron Job

Noting that WP-Cron differs slightly from a system cron and that each has advantages and disadvantages is important. For instance, whereas a system cron relies on specific times, WP-Cron solely depends on intervals.

  • A WP-Cron is also less dependable because it only operates when the page is loaded (either on the front end or the back end). WordPress offers hourly, twice-daily, and daily intervals as defaults.
  • It takes custom hooks to schedule WP-Cron events. The official WordPress plugin manual has an excellent guide on setting up WP Cron events, so we strongly suggest reading it.

We suggest reading Tom Mcfarlin’s article on defining a WordPress cron job if you’re looking for an alternative approach to configuring a system cron with WordPress. For those who are less tech-savvy, we’ll also look at how to use WP-Cron, a well-liked free plugin that complements WP Crontrol. This enables you to view and manage system activity in the WP-Cron.

WP-Cron Schedules

Once activated, you can modify the WordPress Cron job schedules in the “Cron Schedules” section of your dashboard’s settings. Keep in mind that the plugin also adds a default weekly schedule. Additional schedules can also be added in seconds, for example, 21600 seconds every six hours.

WP-Cron Events

Additionally, you can use the plugin to view any currently scheduled WordPress Cron jobs. Select “Cron Events” from the Tools menu. Many of the action names are immediately recognizable because they ought to coincide with a portion of the name of the plugin, for example, “woocommerce_cleanup_sessions” or “gravityforms_cron.”

Simply selecting “Run Now” next to the action name will launch a Cron event immediately. As you might need to run a Cron event several times during troubleshooting, this can be useful.

Change a Cron Event

By selecting “Edit” next to an existing Cron event’s name, you can also edit it. After that, you have the option to modify the action’s name, arguments, next run, and scheduled recurrence. However, be cautious because a lot of plugins depend on their Cron jobs to run properly.

Insert Cron Event

  • Events from Cron can also be added. We’re going to add the WordPress Cron job for the Disqus plugin in this example. If they are experiencing problems with comments syncing back and forth or changing the sync frequency, many people might want to do this.
  • The name of the cron event—or more accurately, the action name—should be listed in the documentation provided by each third-party plugin developer. Specifically, the plugin is using “dsq_sync_forum” in this instance.