TranslateProject/sources/tech/20210412 Scheduling tasks with cron.md
DarkSun 22547cb04d 选题[tech]: 20210412 Scheduling tasks with cron
sources/tech/20210412 Scheduling tasks with cron.md
2021-04-13 05:02:31 +08:00

7.9 KiB
Raw Blame History

Scheduling tasks with cron

Photo by Yomex Owo on Unsplash

Cron is a scheduling daemon that executes tasks at specified intervals. These tasks are called cron jobs and are mostly used to automate system maintenance or administration tasks. For example, you could set a cron job to automate repetitive tasks such as backing up database or data, updating the system with the latest security patches, checking the disk space usage, sending emails, and so on. The cron jobs can be scheduled to run by the minute, hour, day of the month, month, day of the week, or any combination of these.

Some advantages of cron

These are a few of the advantages of using cron jobs:

  • You have much more control over when your job runs i.e. you can control the minute, the hour, the day, etc. when it will execute.
  • It eliminates the need to write the code for the looping and logic of the task and you can shut it off when you no longer need to execute the job.
  • Jobs do not occupy your memory when not executing so you are able to save the memory allocation.
  • If a job fails to execute and exits for some reason it will run again when the proper time comes.

Installing the cron daemon

Luckily Fedora Linux is pre-configured to run important system tasks to keep the system updated. There are several utilities that can run tasks such as cron, anacron, at and batch. This article will focus on the installation of the cron utility only. Cron is installed with the cronie package that also provides the cron services.

To determine if the package is already present or not, use the rpm command:

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

If the cronie package is installed it will return the full name of the cronie package. If you do not have the package present in your system it will say the package is not installed. To install type this:

$ dnf install cronie

Running the cron daemon

A cron job is executed by the crond service based on information from a configuration file. Before adding a job to the configuration file, however, it is necessary to start the crond service, or in some cases install it. What is crond? Crond is the compressed name of cron daemon (crond). To determine if the crond service is running or not, type in 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 something similar including the line “Active: active (running) since…”, you will have to start the crond daemon. To run the crond service in the current session, enter the following command:

$ systemctl run crond.service

To configure the service to start automatically at boot time, type the following:

$ systemctl enable crond.service

If, for some reason, you wish to stop the crond service from running, 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

The cron configuration

Here is an example of the configuration details for a cron job. This defines a simple cron job to pull the latest changes of a 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 is where the timer is set to every 59 minutes.
  • The rest of the line is the command as it would run from the command line. The command itself in this example has three parts:
    • The job will run as the user “username”
    • It will change to the directory /home/username/project/design
    • The git command runs to pull the latest changes in the master branch.

Timing syntax

The timing information is the first part of the cron job string, as mentioned above. This determines how often and when the cron job is going to run. It consists of 5 parts in this order:

  • minute
  • hour
  • day of the month
  • month
  • day of the week

Here is a more graphic way to explain the syntax may be seen here:

.---------------- minute (0 - 59)
 |  .------------- hour (0 - 23)
 |  |  .---------- day of month (1 - 31)
 |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr …
 |  |  |  |  .---- day of week (0-6) (Sunday=0 or 7)
 |  |  |  |  |            OR sun,mon,tue,wed,thr,fri,sat
 |  |  |  |  |
 *  *  *  * user-name  command-to-be-executed

Use of the asterisk

An asterisk (*) may be used in place of a number to represents all possible values for that position. For example, an asterisk in the minute position would make it run every minute. The following examples may help to better understand the syntax.

This cron job will run every minute, all the time:

* * * * [command]

A slash (/) indicates a multiple number of minutes The following example will run 12 times per hour, i.e., every 5 minutes:

*/5 * * * * [command]

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

0 0 2 * * [command]

Using crontab to create a cron job

Cron jobs run in the background and 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 are not supposed to be edited directly. The crontab command is the method you use to create, edit, install, uninstall, and list cron jobs.

The same crontab command is used for creating and editing cron jobs. And whats even cooler is that you dont need to restart cron after creating new files or editing existing ones.

$ crontab -e

This opens your existing crontab file or creates one if necessary. The vi editor opens by default when calling crontab -e. Note: To edit the crontab file using Nano editor, you can optionally set the EDITOR=nano environment variable.

List all your cron jobs using the option -l and specify a user using the -u option, if desired.

$ crontab -l
$ crontab -u username -l

Remove or erase all your cron jobs using the following command:

$ crontab -r

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

$ crontab -r -u username

Thank you for reading. cron jobs may seem like a tool just for system admins, but they are actually relevant to many kinds of web applications and user tasks.

Reference

Fedora Linux documentation for Automated Tasks


via: https://fedoramagazine.org/scheduling-tasks-with-cron/

作者:Darshna Das 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出