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

How to correctly use the crond command of the Linux task scheduling process

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

Share

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

This article mainly explains "how to correctly use the crond command of the Linux task scheduling process". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to correctly use the crond command of the Linux task scheduling process".

A brief introduction to crond

Concept

The concept of crond and crontab are inseparable. Crontab is a command, commonly found in Unix and Unix-like operating systems, that sets instructions that are executed periodically. This command reads instructions from a standard input device and stores them in a "crontab" file for later reading and execution. The word comes from the Greek chronos, which originally means time. And crond is its daemon.

Crond is a daemon used under linux to periodically execute certain tasks or wait for certain events to be handled. Similar to the scheduled tasks under windows, when the installation of the operating system is completed, this service tool is installed by default, and the crond process is automatically started. The crond process periodically checks whether there is a task to be executed every minute, and automatically executes the task if there is a task to be executed.

Task scheduling under Linux is divided into two categories: system task scheduling and user task scheduling.

L system task scheduling: the work that the system periodically performs, such as writing cached data to the hard disk, log cleaning, etc. There is a crontab file in the / etc directory, which is the configuration file for system task scheduling.

The / etc/crontab file includes the following lines:

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

01 * root run-parts / etc/cron.hourly02 4 * root run-parts / etc/cron.daily22 4 * * 0 root run-parts / etc/cron.weekly42 4 1 * * root run-parts / etc/cron.monthly the first four lines are the environment variables used to configure the crond task to run. The first line SHELL variable specifies which shell the system should use, here is bash, and the second line PATH variable specifies the path to which the system executes the command. The third line of the MAILTO variable specifies that the task execution information of crond will be emailed to the root user, if the value of the MAILTO variable is empty, the task execution information will not be sent to the user, and the HOME variable of the fourth line specifies the home directory to be used when executing the command or script. The meaning expressed by the sixth to ninth lines will be described in detail in the next section. There's no more to say here.

User task scheduling: tasks that users perform on a regular basis, such as user data backup, scheduled email reminders, etc. Users can use the crontab tool to customize their scheduled tasks. All user-defined crontab files are saved in the / var/spool/cron directory. Its file name is the same as the user name.

II. The use of crontab tools

(1) the format of crontab

There are two commonly used formats for crontab:

Crontab [- u user] [file]

Crontab [- u user] [- e |-l |-r |-I]

The meaning of the option is as follows:

L-u user: the crontab service used to set a user. For example, "- u ixdba" means to set the crontab service for the ixdba user. This parameter is usually run by the root user.

L file:file is the name of the command file, indicating that file is used as a task list file for crontab and loaded into crontab. If this file is not specified on the command line, the crontab command accepts commands typed on the standard input (keyboard) and loads them into crontab.

L-e: edit the contents of a user's crontab file. If you do not specify a user, you edit the current user's crontab file.

L-l: displays the contents of a user's crontab file. If no user is specified, it shows the contents of the current user's crontab file.

L-r: deletes a user's crontab file from the / var/spool/cron directory. If no user is specified, the current user's crontab file is deleted by default.

L-I: give a confirmation prompt when deleting the user's crontab file.

(2) the meaning of crontab file

In the crontab file created by the user, each line represents a task, and each field on each line represents a setting. Its format is divided into six fields. The first five paragraphs are the time setting section, and the sixth paragraph is the command segment to be executed. The format is as follows:

Minute hour day month week command where:

L minute: represents minutes and can be any integer from 0 to 59.

L hour: represents the hour and can be any integer from 0 to 23.

L day: represents the date and can be any integer from 1 to 31.

L month: represents the month and can be any integer from 1 to 12.

L week: represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.

L command: the command to be executed can be either a system command or a script file written by yourself.

The following special characters can also be used in each of the above fields:

L asterisk (*): represents all possible values. For example, if the month field is an asterisk, it means that the command operation is performed every month after the constraints of other fields are met.

L comma (,): you can specify a list range with values separated by commas, for example, "1pens 2pens 5pencils 7pas 8pas 9"

L middle bar (-): you can use the middle bar between integers to represent a range of integers, for example, "2-6" means "2par 3pr 4je 5je 6".

L forward slash (/): you can specify the interval frequency of the time with a forward slash, for example, "0-23 ramp 2" indicates that it is executed every two hours. At the same time, a forward slash can be used with an asterisk, such as * / 10, which, if used in the minute field, means that it is executed every ten minutes.

(3) examples of crontab documents

0 * / 3 * / usr/local/apache2/apachectl restart means to restart the apache service every 3 hours.

30 3 * * 6 / webdata/bin/backup.sh

Represents the execution of the / webdata/bin/backup.sh script at 03:30 every Saturday.

0 01 fsck / dev/sdb8 20 * *

Indicates the 1st and 20th of each month to check / dev/sdb8 disk devices.

10 5 * / 5 * * echo "" > / usr/local/apache2/log/access_log means that the apache log is cleaned at 05:10 on the 5th, 10th, 15th, 20th, 25th and 30th of each month.

Matters needing attention in using crontab tools

(1) pay attention to the problem of environmental variables

Sometimes we create a crontab, but the task cannot be performed automatically, but there is no problem with performing the task manually, usually because there are no environment variables configured in the crontab file.

When defining multiple scheduled tasks in the crontab file, one problem that needs to be paid special attention to is the setting of environment variables, because when we execute a task manually, it is carried out in the current shell environment, and the program can of course find the environment variables, but when the system automatically executes task scheduling, it will not load any environment variables, so it is necessary to specify all the environment variables needed for the task to run in the crontab file. There is no problem when the system performs task scheduling.

(2) pay attention to cleaning the mail logs of system users

When each task is scheduled and executed, the system will send the task output information to the current system user in the form of e-mail. Over time, the log information will be very large, which may affect the normal operation of the system. Therefore, it is very important to redirect each task.

For example, you can set the following form in the crontab file to ignore the log output:

0 * / 3 * / usr/local/apache2/apachectl restart > / dev/null 2 > & 1 "/ dev/null 2 > & 1" means that the standard output is redirected to / dev/null, and then the standard error is redirected to the standard output. Because the standard output has been redirected to / dev/null, the standard error will also be redirected to / dev/null, so the log output problem is solved.

(3) system-level task scheduling and user-level task scheduling

System-level task scheduling mainly completes some maintenance operations of the system, and user-level task scheduling mainly completes some user-defined tasks. User-level task scheduling can be completed by system-level task scheduling (not recommended), but the reverse is not recommended. Root users' task scheduling operations can be set through "crontab-uroot-e", or scheduling tasks can be directly written to / etc/crontab file. It is important to note that if you want to define a task to periodically restart the system, you must put the task in the / etc/crontab file, even if the task of creating a scheduled restart system under the root user is invalid.

Thank you for your reading, the above is the content of "how to correctly use the crond command of the Linux task scheduling process". After the study of this article, I believe you have a deeper understanding of how to correctly use the crond command of the Linux task scheduling process. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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