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 use Crontab to schedule tasks in Linux

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

Share

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

In this issue, the editor will bring you about how to use Crontab timing tasks in Linux. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

A brief introduction to crond

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.

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:

Cat / etc/crontab

SHELL=/bin/bash

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

MAILTO=HOME=/

The first four lines are the environment variables used to configure the crond task to run.

The first line of the SHELL variable specifies which shell the system will use, here is bash

The second line PATH variable specifies the path where the system executes the command

The third line MAILTO variable specifies that the task execution information of crond will be sent to the root user by email. If the value of the MAILTO variable is empty, the task execution information will not be sent to the user.

The HOME variable on the fourth line specifies the home directory to be used when executing a command or script.

The meaning of the 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:

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

Hour: represents an hour, which can be any integer from 0 to 23.

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

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

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

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

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

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.

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

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

Forward slash (/): the interval frequency of the time can be specified with a forward slash, for example, "0-23gam2" 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.

II. Crond services

Start the service

/ bin/systemctl start crond.service

3. Configure regular execution of myshell scripts

1. First create a .sh file under a folder and grant 755 permissions to the permissions file

Here my sh file path is / data0/myshell/conllect5m.sh

Then give permissions to the file

Chmod-R 755 / data0/myshell/

In this way, all files under myshell have 755 permissions.

Then open the conllect5m.sh file with the vi command

two。 Switch to directory

Cd / data0/myshell

Use vi to edit files

Vi collect5m.sh

The code is as follows: the first line is required, the second line is the description, and the third line configures the address to execute the scheduled task

#! / bin/sh#descripion: describe wget http://www.baidu.com

After writing, enter Esc to switch to command mode, enter: wq to save edits and exit

3. Let's change to the directory / etc

Use vi to edit the file vi crontab

Enter the following command

SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root # For details see man 4 crontabs # Example of job definition:#.-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,thu,fri Sat# | # * user-name command to be execute # perform scheduled tasks * / 5 * root / data0/myshell/collect5m.sh#*/10 * root / data0/myshell/collect10m.sh#*/15 * root / data0/myshell/collect15m.sh#*/20 * root / data0/myshell/collect20m every 5 minutes. Sh#*/30 * root / data0/myshell/collect30m.sh

After writing, enter Esc to switch to command mode, enter: wq to save edits and exit

Then execute the command to start crontab

/ bin/systemctl start crond.service above is what Xiaobian shared with you about how to use Crontab to schedule tasks in Linux. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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