Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of scheduling tasks using cron

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the method of using cron to schedule tasks". In the daily operation, I believe that many people have doubts about the method of using cron to schedule tasks. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "using cron to schedule tasks". Next, please follow the editor to study!

Cron is a scheduling daemon that executes tasks at specified intervals. These tasks, called corn jobs, are mainly used to automate system maintenance or administrative tasks. For example, you can set up a cron job to automate repetitive tasks, such as backing up databases or data, updating the system with the latest security patches, checking disk space usage, sending emails, and so on. Cron jobs can be run in minutes, hours, days, months, weeks, or any combination of them.

Some advantages of cron

Here are some of the advantages of using cron jobs:

You can better control the running time of your homework. For example, you can be accurate to minutes, hours, days, etc.

It eliminates the need to write code to loop the task logic, and you can turn it off when you no longer need to perform the task.

Jobs do not take up memory when they are not executed, so you can save memory allocation.

If a job fails and exits for some reason, it will run again at the appropriate time.

Install the cron daemon

Fortunately, Fedora Linux is preconfigured to run important system tasks to keep the system updated, and there are several utilities that can run tasks such as cron, anacron, at, and batch. This article focuses only on the installation of the cron utility. Cron is installed with the cronie package, and the cronie package also provides cron services.

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 returns the full name of the cronie package. If it is not installed on your system, it will show that it is not installed.

Use the following command to install:

$dnf install cronie runs the cron daemon

The cron job is executed by the crond service, which reads the information in the configuration file. Before adding a job to the configuration file, you must start the crond service or install it. What is crond? Crond is short for 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 don't see something 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 boot automatically, 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 defines a cron job cron configuration

The following is an example of the configuration details of a cron job. It defines a simple cron job that pulls the latest changes from the git master branch to the clone's 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 execute once in the 59th minute. (LCTT translation note: there is an error here in the original text. )

The rest of the line is a command because it will be run from the command line. In this example, the command itself consists of three parts:

The job will run as user username

It will change to the directory / home/username/project/design

Run the git command to pull the latest changes in the master branch

Time grammar

As mentioned above, the time information is the first part of the cron job string, as shown above. It determines how often and when cron jobs run. It consists of five parts in the following order:

Minute

Hour

One day of the month

Month

One day of the week

Here is a more graphical way to explain the syntax:

.-minutes (0-59) |.-hours (0-23) | |.-one day in January (1-31) |.-month (1-12) or jan, feb, mar, apr... | |.-day of the week (0-6) (Sunday = 0 or 7) | or sun, mon, tue, wed, thr, fri, sat | * use of the user-name command-to-be-executed asterisk |

An asterisk (*) can be used instead of a number to indicate all possible values for that location. For example, the asterisk in the minute position causes it to run every minute. The following example may help to better understand the syntax.

This cron job will run every minute:

* [command]

The slash represents the number of minutes between minutes. The following example will run 12 times per hour, that is, every 5 minutes:

* / 5 * [command]

The next example will be midnight on the second day of each month (for example, 12: 00 a.m. on January 2, 12:00 on February 2, etc.):

0 02 * [command]

(LCTT translation note: for cron time format, there are more format symbols, which are not expanded here)

Create a cron job using crontab

The cron job runs in the background and constantly checks the / etc/crontab file and / etc/cron.*/ and / var/spool/cron/ directories. Each user has a unique crontab file in / var/spool/cron/.

You should not edit these cron files directly. The crontab command is a method for creating, editing, installing, uninstalling, and listing cron jobs.

What's even cooler is that you don't have to restart cron after creating a new file or editing an existing file.

$crontab-e

This will open your existing crontab file, or create one. When you call crontab-e, the vi editor is used by default. Note: to edit the crontab file using Nano, you can set the EDITOR=nano environment variable.

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

$crontab-l $crontab-u username-l

Delete all cron jobs using the following command:

$crontab-r

To delete a job for a specific user, you must run the following command as root:

$crontab-r-u username

Thank you for your reading. The cron job may seem like a tool for system administrators, but it is actually related to many Web applications and user tasks.

At this point, the study on "the method of scheduling tasks using cron" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report