linux-crontab scheduled execution/scheduling of scheduled tasks every minute

I. Introduction

This article explains how to call scheduled tasks on Linux, such as printing logs every minute, executing log cutting scripts at 24 o'clock every day, etc.

2. Overview of crontab

2.1. Introduction to crontab command

In Linux systems, the crontab command is a command used to execute scheduled tasks crond(crontab)是系统默认自带的定时服务. We can set up scheduled tasks by editing the crontab file so that the system can automatically execute commands according to the set time and frequency. Crontab files are usually saved in the /etc/crontab and /var/spool/cron directories.
Insert image description here

Summary: The crontab command can only be used after the crond service is installed. The crontab command edits scheduled tasks in the specified format and saves them in the crontab file. The crond service will periodically check the scheduled task list in the crontab file every minute to execute the task.

2.2. Classification of Linux scheduled tasks

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

System task scheduling : work that the system performs periodically, 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.

/etc/crontabThe file includes the following lines:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=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 executed

User task scheduling : User-defined tasks to be performed regularly. Users can use the crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the /var/spool/cron directory. The file name is consistent with the user name. For example, if I set up a scheduled task as the root user, then the corresponding crontab file is /var/spool/cron/root.

The crontab command allows users to submit, edit or delete corresponding jobs. Each user can have a crontab file to save scheduling information. System administrators can disable or allow them through the cron.deny and cron.allow files.

/etc/cron.deny     该文件中所列用户不允许使用crontab命令
/etc/cron.allow    该文件中所列用户允许使用crontab命令
/var/spool/cron/   所有用户crontab文件存放的目录,以用户名命名

Summary: The system-level scheduled task crontab file is /etc/crontab. The crontab file of user-defined scheduled tasks is saved in the /var/spool/cron/ directory, and the name is consistent with the user name.

3. Install crontab

1. Check whether the crond service is installed on the system

service crond status

Insert image description here

2.Installation

yum -y install vixie-cron
yum -y install crontabs

Note:
The vixie-cron package is the main program of cron;
the crontabs package is a program used to install, uninstall, or list tables used to drive the cron daemon.

3. Start the service
cron is a built-in service of Linux, but it does not start automatically. You can use the following methods to start and shut down the service:

service crond start     //启动服务
service crond stop      //关闭服务
service crond restart   //重启服务
service crond reload    //重新载入配置
service crond status    //查看crontab服务状态

4. Set up automatic startup at boot

ntsysv #查看crontab服务是否已设置为开机启动
chkconfig –level 35 crond on #加入开机自动启动
chkconfig –list crond //开机级别的crond服务运行情况 ,2、3、4、5级别开机会自动启动crond服务
chkconfig crond off//取消开机自动启动crond服务:

4. Use of crontab

4.1. crontab syntax

 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Common options:

Options Function
-u user Used to set the crontab service of a certain user. For example, -u test means setting the crontab service of the test user. This option is generally only the root user has permission to run.
-e Edit the contents of a user's crontab file. If no user is specified, it means editing the current user's crontab file.
-l Displays the contents of a user's crontab file. If no user is specified, it displays the contents of the current user's crontab file.
-r Delete a user's crontab file from /var/spool/cron. If no user is specified, the current user's crontab file will be deleted by default.
-i When deleting the user's crontab file, give a confirmation prompt
语法:
crontab [-u username] [-l|-e|-r]

示例:
# 用root用户执行以下命令,给test用户创建定时任务
crontab -u test -e

# root用户要查看自己的详细crond服务内容
命令:crontab -u root -l

Note: In the crontab [option] file command, file refers to the name of the command file, which means that file is used as the task list file of crontab and loaded into crontab. If the file name is not specified in the command line, this command will accept the standard Enter the commands typed on the keyboard and type them into crontab, for example: crontab -u test /root/jobs.txt means to load crontab with the task list in the /root/jobs.txt file.

4.2. Scheduled task settings

1. Directly enter the command crontab -e or edit the file /etc/crontab to directly set the scheduled task.

crontab -e   //若命令是root用户执行,建议直接使用编辑/etc/crontab的方式
或者
vi /etc/crontab

Note: crontab -e will enter the editing mode. When the entered command format is correct, it will be saved to the /var/spool/cron directory and named after the current user;

Insert image description here

Here we explain the system task scheduling mode, that is, editing the /etc/crontab file

1. 输入 vi /etc/crontab
2. 按下 i 键进入到编辑模式
3. 输入定时命令 (文件路径必须是绝对路径)
0 */1 * * * /home/work/start-service.sh
4. 然后按下 esc 退出编辑模式
5. 按下 shift+: 输入 wq 退出 crontab
6. 输入以下命令使任务生效
crontab /etc/crontab
7. 查看当前系统中的定时任务列表
crontab -l

Insert image description here

4.3. Scheduled task format

We see that /etc/crontabthe file has the following format

# 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 executed
 分  时 日 月 周 [用户名]    <要执行的命令>
分钟 小时 日期 月份 星期 (crontab表达式)
user-name 指的是执行这个定时任务的用户
command to be executed : 运行的命令

示例:每分钟以root用户执行/root/1.sh脚本
* * * * * root bash /root/1.sh

The meaning of each field is as follows:

  • minute: represents the minute, which can be any integer from 0 to 59.
  • hour: represents the hour, which can be any integer from 0 to 23.
  • day: represents the date, which can be any integer from 1 to 31.
  • month: represents the month, which can be any integer from 1 to 12.
  • week: Indicates 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.

In each of the above fields, you can also use the following special characters:

  • Asterisk (*): represents all possible values. For example, if the month field is an asterisk, it means that the command operation will be executed every month after the constraints of other fields are met.
  • Comma (,): You can specify a list range with comma-separated values, for example, "1,2,5,7,8,9"
  • Center bar (-): You can use a center bar between integers to represent an integer range, for example, "2-6" means "2,3,4,5,6"
  • Forward slash (/): You can use forward slash to specify the time interval frequency, for example, "0-23/2" means to execute every two hours. At the same time, forward slashes can be used together with asterisks, such as */10. If used in the minute field, it means that it will be executed every ten minutes.

Note: There is only "absolute path" in the crontab command, and there is no relative path, so you need to write the absolute path to execute any command.

4.4 Online verification of crontab expression

https://tool.lu/crontab
Linux crontab command | Newbie tutorial

5. Examples

Case 1: Required to execute the 1.sh script as root user once every minute

* * * * * root /bin/bash /root/1.sh

Case 2: It is required to execute the touch /tmp/testfile.txt command once every day at 3 a.m.

0 3 * * * /usr/bin/touch /tmp/testfile.txt

Case 3: Required to be executed once every day at 7, 9, and 12 am

0 7,9,12 * * *  /app/Projects/bin/start.sh -config=/data/application.yml >> /data/info.log 2>&1

Case 4: Required to be executed every 2 minutes at 7 am every day (executed at 0 minutes)

*/7 7 * * *  echo "Have a good dream" >> /tmp/test.txt

Case 5: Required to be executed on Monday, the 1st of each month

0 0 1 * 1

Case 6: Required to be executed every 5 minutes from 8 to 11 o'clock every day

*/5 8,9,10,11 * * *
或者

*/5 8-11 * * *

Case 7: It is required to execute every 5 minutes from 21:00 to 3:00 every day.

*/5 21-3 * * *
或者

*/5 21-23,0-3 * * *

Case 8: It is required to start at 7 o'clock every day and execute it every two minutes

*/2 7-23 * * *

Case 9: It is required to start at 7 o'clock every day and execute it every two minutes every two hours.

*/2 7-23/2 * * *

Case 10: Required to be executed once at 7:00 and 2:00 pm on working days (1-5)

0 7,14 * * 1,2,3,4,5

6. Log of scheduled tasks

When an error occurs in a scheduled task and you need to troubleshoot the error, you need to go to the scheduled task log file to query the cause of the error.

/var/log/cron : 可以查看到执行那些定时任务
/var/log/messages: 记录系统操作的日志,(例如:用户登录)

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/133083835
Recommended