baicai

白菜

一个勤奋的代码搬运工!

Schedule automation tasks using cron.

cron is a scheduling daemon that executes tasks at specified time intervals. These tasks, known as cron jobs, are primarily used for automating system maintenance or management tasks. For example, you can set up a cron job to automatically perform repetitive tasks such as backing up databases or data, updating the system with the latest security patches, checking disk space usage, sending emails, and more. Cron jobs can be scheduled to run every minute, hour, day, month, week, or any combination of these.

Some advantages of cron#

Here are some advantages of using cron jobs:

You have better control over the execution time of jobs. For example, you can specify the exact minute, hour, day, etc.
It eliminates the need to write code for loop task logic, and you can simply turn it off when you no longer need to execute the task.
Jobs do not occupy memory when they are not running, so you can save memory allocation.
If a job fails to execute and exits for some reason, it will run again at the appropriate time.

Installing the cron daemon#

Fortunately, Fedora Linux comes pre-configured with utilities to run important system tasks to keep the system up to date, and there are several utilities available to run tasks such as cron, anacron, at, and batch. This article focuses only on the installation of the cron utility. The cron and cronie packages are installed together, and the cronie package also provides the cron service.

To determine if the package already exists, use the rpm command:#

$ rpm -q cronie Cronie-1.5.2-4.el8.x86_64

If cronie is installed, it will return the full name of the cronie package. If it is not installed on your system, it will display "not installed".

Use the following command to install:#

$ dnf install cronie

Running the cron daemon#

Cron jobs are executed by the crond service, which reads the information from the configuration file. Before adding jobs to the configuration file, you must start the crond service or install it. What is crond? Crond is the abbreviation for the cron daemon. To determine if the crond service is running, enter the following command:

$ systemctl status crond.service

● crond.service - Command Scheduler
    Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor pre>
    Active: active (running) since Sat 2021-03-20 14:12:35 PDT; 1 day 21h ago
    Main PID: 1110 (crond)

If you do not see similar content like Active: active (running) since…, you need to start the crond daemon. To run the crond service in the current session, enter the following command:

$ systemctl run crond.service

To configure it to start automatically at boot, enter the following command:

$ systemctl enable crond.service

If for some reason you want to stop the crond service, use the stop command as follows:

$ systemctl stop crond.service

To restart it, simply use the restart command:

$ systemctl restart crond.service

Defining a cron job#

cron configuration#

Here is an example of the configuration details for a cron job. It defines a simple cron job that pulls the latest changes from the git master branch into a cloned repository:

*/59 * * * * username cd /home/username/project/design && git pull origin master

There are two main parts:

The first part is */59 * * * *. This indicates that the timer is set to run every 59th minute.
The rest of the line is the command, as it will be run from the command line. In this example, the command itself consists of three parts:
    The job will run as the user username
    It will switch to the directory /home/username/project/design
    It will run the git command to pull the latest changes from the master branch

Time syntax#

As mentioned above, the time information is the first part of the cron job string, as mentioned above. It determines the frequency and time at which the cron job runs. It includes 5 parts in the following order:

  • Minute
  • Hour
  • Day of the month
  • Month
  • Day of the week

Here is a more graphical way to explain the syntax:

.--------------- Minute (0 - 59)
|  .------------- Hour (0 - 23)
|  |  .---------- Day of the month (1 - 31)
|  |  |  .------- Month (1 - 12) or jan, feb, mar, apr, etc.
|  |  |  |  .---- Day of the week (0-6) (Sunday=0 or 7)
|  |  |  |  |            or sun, mon, tue, wed, thu, fri, sat
|  |  |  |  |               
*  *  *  *  *  user-name  command-to-be-executed 

Using asterisks#

Asterisks (*) can be used to substitute numbers, representing all possible values in that position. For example, an asterisk in the minute position will make it run every minute. The following examples may help to better understand the syntax.

This cron job will run every minute:

* * * * [command]

The slash represents the interval number for minutes. The following example will run 12 times per hour, i.e., every 5 minutes:

*/5 * * * * [command]

The next example will run at midnight on the second day of every month (e.g., January 2nd at 12:00 AM, February 2nd at 12:00 AM, etc.):

0 0 2 * * [command]

There are more format symbols for cron time format, which are not expanded here.

Creating a cron job using crontab#

Cron jobs run in the background, and they constantly check the /etc/crontab file and the /etc/cron.*/ and /var/spool/cron/ directories. Each user has a unique crontab file in /var/spool/cron/.

These cron files should not be edited directly. The crontab command is used as a method to create, edit, install, uninstall, and list cron jobs.

Even cooler, you don't need to restart cron after creating a new file or editing an existing one.

$ crontab -e

This will open your existing crontab file or create one. By default, when calling crontab -e, the vi editor is used. Note: To use the Nano editor to edit the crontab file, you can set the EDITOR=nano environment variable.

Use the -l option to list all cron jobs. If needed, use the -u option to specify a user.

$ crontab -l

$ crontab -u username -l

Use the following command to remove all cron jobs:

$ crontab -r

To remove jobs for a specific user, you must run the following command as the root user:

$ crontab -r -u username

Cron jobs may seem like tools for system administrators only, but they are actually relevant to many web applications and user tasks.

References#

[1] Fedora Linux documentation

[2] Scheduling tasks using cron

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.