[Linux server operation and maintenance] Detailed explanation of scheduled task crontab | Book delivery at the end of the article

Preface

The main contents outlined in this article’s mind map:

Insert image description here

1. Introduction to crontab

1.1 What is crontab

Crontab is a tool for timing tasks on Unix and Linux operating systems . It allows users to create and manage scheduled tasks to automatically run commands or scripts at specific intervals or points in time. Crontab is cron tablethe abbreviation of , cronwhich refers to a background process in Unix systems that is used to perform scheduled tasks.

A Crontab file contains a series of scheduled task entries, each of which defines when a task should be executed and the command or script to be run. These tasks can be scheduled in different time units such as minutes, hours, days, months, and weeks.

1.2 crontab command workflow

The crontab command is usually used to set up regularly executed tasks in Linux systems and store these tasks in crontab files. In order to use the crontab command, the system requires dependent crond 服务support. Normally, when the operating system is installed, the crond service is installed by default and starts automatically. The crond process will regularly check the task list in the crontab file every minute to determine whether there are tasks that need to be performed. If so, it will automatically perform these tasks.

The following topology diagram is easy to understand:

Insert image description here

It can be summarized as follows: the crontab command can only be used after the crond service is installed. The crontab command edits scheduled tasks according to 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.

1.3 Linux scheduled task classification

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.
  • 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 directory /var/spool/cron. The file name is consistent with the user name. For example, if I set up a scheduled task with the test user, then the corresponding crontab file is /var/spool/cron/test.

2. Detailed explanation of crontab usage

2.1 crond service installation

Under normal circumstances, the crond service will be installed by default and run automatically when installing the system.

Check whether the service is installed under centos or Red Hat system:

systemctl status crond

If Unit crond.service could not be found. is displayed, the crond service needs to be installed.

# crond 安装:
yum -y install crontabs
# 启动 crond 服务: 
systemctl start crond
# 关闭 crond 服务: 
systemctl stop crond
# crond设置开机自启动: 
systemctl enable crond
# 重新载入配置
systemctl reload crond
# 查看 crontab 服务是否已经加入了开机启动
chkconfig crond --list
# 加入开机自动启动
chkconfig crond on

2.2 crontab file content analysis

As mentioned before, we have learned about using the crontab command to edit and save scheduled tasks into a crontab file. Now, let's take a closer look at the format of a scheduled task.

You can execute the following command to view the sample contents of the crontab file:

Insert image description here

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

The SHELL variable in the first line specifies which shell the system should use, here it is bash

The PATH variable in the second line specifies the path for the system to execute the command.

The MAILTO variable in the third line specifies that crond's task execution information will be sent to the root user via email.

If the value of the MAILTO variable is empty, it means that task execution information will not be sent to the user.

The last line is the Crontab entry. The basic format is as follows:

*  *  *  *  * user-name  command to be executed

The execution time of the task is determined by setting these five in the crontab file *. user-name is the user who executes the task, and command to be executed is the command or script task to be executed. Let’s take a closer look at what these five represent *. meaning.

Among them, the asterisk represents a wildcard, which means it can match any value. Each asterisk represents a different unit of time:

  • The first asterisk represents the minute (0-59)
  • The second asterisk indicates the hour (0-23)
  • The third asterisk represents the day of the month (1-31)
  • The fourth asterisk represents the month (1-12 or use abbreviation, such as 1 for January, 2 for February)
  • The fifth asterisk represents the day of the week (0-7 or abbreviated, 0 and 7 both represent Sunday, 1 represents Monday, and so on)

In fact, in addition to *this character, there are other special characters used to meet different timing needs.

special symbols meaning for example
*(Asterisk) means any time For example: * 8 * * * means to execute the command every minute at 8 o'clock every day
,(comma) Use commas to separate the values ​​of the field. For example: 10 8,10,12 * * *it means that the command will be executed every day at 8:10, 10:10, and 12:10
-(middle bar) Represents the range of values ​​between two integers For example: 10 8 * * 1-3 means to execute the command once every Monday to Wednesday at 8:10
/ (forward slash) Indicates how often to execute For example: * /10 * * * *means to execute the command every 10 minutes

Summarize:

  • It is best not to use week and day at the same time
  • Timed tasks need to be annotated
  • Can be directed to a log file or an empty file
  • The scheduled task must be an absolute path, and the directory must exist to produce results.
  • The crontab service must be started and running

2.3 crontab command usage

2.3.1 View scheduled task list

Use the following command to view the scheduled task list

[test@hecs]# crontab -l	# 查看定时任务列表

2.3.2 Edit/Create scheduled tasks

In fact, in use, setting up Crontab scheduled tasks is very easy. Just switch to the user who executed the command and run crontab -ethe command. This will open a blank file where you can simply enter the task you want to schedule.

[test@hecs]# crontab -e	# 编辑定时任务
#进入 crontab 编辑界面。会打开Vim编辑你的任务
 */10 * * * *   /home/test/test.sh

/var/spool/cron/After editing and saving, a file named test will be generated in the directory. This file is the crontab file of the test user.

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 on the command line, this command will accept standard input ( 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.

2.3.3 Delete scheduled tasks

# 直接删除不提示
[test@hecs]# crontab -r # 删除前给出提示确认
[test@hecs]# crontab -i

After executing the above command, /var/spool/cronthe user's crontab file will be deleted from the directory. If no user is specified, the current user's crontab file will be deleted by default.

Note : After executing the delete command, all scheduled tasks under this user will be deleted. If you just want to delete a certain scheduled task,crontab -ejust use the command to edit the crontab file and delete a certain task.

2.3.4 Other crontab related operations

(1) Back up crontab file

Execute the following command to back up the current crontab file

crontab -l > $HOME/mycron
(2) Recover lost crontab files

If you accidentally delete the crontab file, assuming you $HOMEstill have a backup in your own directory, you can copy it to /var/spool/cron/<username>, where is the user name. If the copy cannot be completed due to permission issues, you can use: crontab where is $HOMEthe file name of your copy in the directory.

Some crontab variations are a bit weird, so be careful when using the crontab command. If any options are omitted, crontab may open an empty file, or appear to be an empty file. Exit directly at this time, do not press Ctrl-D, otherwise you will lose the crontab file.

3. Precautions for crontab

3.1 Pay attention to environment variables

When defining multiple scheduling tasks in a crontab file, one issue that requires special attention is the setting of environment variables.

When the file path is involved in the script, write the global path;
when the script execution requires java or other environment variables, introduce the environment variables through the source command, such as:

cat start_cbp.sh
#!/bin/sh
source /etc/profile
export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf
/usr/local/jboss-4.0.5/bin/run.sh -c mev &

When manually executing the script is OK, but the crontab is not executed. You can try to directly introduce environment variables in crontab to solve the problem.

0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh

3.2 System-level task scheduling and user-level task scheduling

The root user's task scheduling operation can be crontab -uroot -eset through , or the /etc/crontabscheduled task can be written directly to the file. It should be noted that if you want to define a task to restart the system regularly, you must put the task into /etc/crontabthe file, even if it is created under the root user A scheduled system restart task is also invalid.

3.3 Other notes and summary

  1. Environment variable problems, for example, crontab cannot recognize Java environment variables.
    When crontab executes the shell, it can only recognize a few environment variables. Ordinary environment variables cannot be recognized, so when writing a shell, it is best to use export to re-declare the variables. Make sure the script executes.
  2. It is best to use scripts to execute commands
  3. Add /bin/sh to script permissions, and standard path /server/scripts
  4. Time variables are escaped with backslashes, preferably in scripts
  5. Add comments to scheduled tasks
  6. >/dev/null 2>&1 ==>&>/dev/nullDon’t print log files randomly
  7. The scheduled task must be an absolute path, and the directory must exist to produce results.
  8. Avoid unnecessary programs and command output
  9. Add comments before scheduled tasks
  10. Package to the upper level of the file directory
  11. It is best not to use week and day at the same time
  12. Can be directed to a log file or an empty file
  13. The crontab service must be started and running

4. Debugging ineffective/invalid crontab

4.1 Solution to failed crontab debugging

When crontab suddenly fails, you can try /etc/init.d/crond restartto solve the problem. Or check the log to see if a job is executed/error reported tail -f /var/log/cron.
Don't run it randomly crontab -r, as this will delete the user's crontab file from the crontab directory (/var/spool/cron). After deleting all the crontabs of the user, they are gone.
It has a special meaning in crontab %, which means line break. If you want to use it, you must escape it \%. If it is often used, date +%Y%m%dit will not be executed in crontab and should be replaced withdate +\%Y\%m\%d

4.2 Reasons and solutions for scheduled tasks not being executed

The format of the scheduled task settings is correct, and manual execution is no problem, but the scheduled task is not executed. This situation is generally caused by the following reasons.

  • The crond service is not started.
    You need to first check whether the crond service is started. You can use the systemctl status crond command to check. If it is not started, just start it.
  • The script has no permissions.
    If you want to execute the script without permissions, just add permissions through the chmod command.
  • File path problem:
    When the file path is involved in the script to be executed by the scheduled task, the global path must be written, and the relative path cannot be written.
  • Environment variable issues:
    When the script to be executed by a scheduled task uses java or other environment variables, the environment variables need to be introduced through the source command.
[test@vm1]# cat test.sh
#!/bin/bash
source /etc/profile
java -jar  /home/test/test.jar 

At the end of the article, I give you a book "Learn Linux Quickly: System Application from Beginner to Master"

What the blogger recommends today is this book "Learn Linux Quickly: System Applications from Beginner to Mastery" written by Mr. Liangxu, the leading figure in the Linux field.

  • How to participate: Follow the blogger and leave a message in the comment area to participate

  • Quantity given out: Tentatively 1~3 copies will be given out to fans

A book is the key to opening the door to Linux learning! !

Insert image description here

Reasons recommended by bloggers

If you are a novice who has just started learning Linux, I believe you have realized that compared with learning a programming language, the threshold for learning the Linux system is relatively high, and you will encounter some confusion, such as:

  • Why should we learn Linux? In what fields can we show our talents after completing the course?

  • Since the birth of Linux, there have been hundreds of distribution versions with different characteristics. How should we choose?

  • There are a lot of complex concepts and instructions, which seems overwhelming...

Then this book is perfect for you, it can be regarded as the key to opening the door to Linux learning!

  • If you are a novice who has just started learning Linux, then this book can be used as an introductory guide to help you get started with Linux quickly.

  • If you want to get more value-for-money content, this book provides you with 150 teaching videos + electronic lesson plans + learning materials, and 5 high-quality online courses worth 50 yuan.

  • If you want to gain more practical experience, this book provides 47 knowledge expansion and 220 hands-on exercises.

To learn Linux quickly, you can really try this book!

Introduction to this book

PART1: Why learn Linux system

The most direct reason is that Linux is widely used and needs to be used in many aspects of practical work. In the field of server applications in large, medium and small enterprises, the market share of Linux systems is getting heavier and heavier, which also illustrates the outstanding performance and widespread application of Linux.

Linux is a multi-user, multi-tasking, multi-threading and multi-CPU operating system based on POSIX and UNIX, and is free to use and spread freely. Users can obtain it for free through the Internet or other channels, and can modify its source code at will, which is what distinguishes it from other operating systems.

Linux is the most famous example of free software and open source software development. As long as the GNU GPL (GNU General Public License) is followed, any individual or institution can freely use all the underlying source code of Linux, and can also modify and redistribute it freely. Therefore, Linux has also become synonymous with open source software.

PART2: Application fields of Linux systems

Currently, various Linux distributions are used in many situations ranging from embedded devices to supercomputers. Especially in the field of IT servers, Linux has established a dominant position, as shown in the figure.

Insert image description here

The server generally uses LAMP (Linux+Apache+MySQL+PHP) or LNMP (Linux+Nginx+MySQL+PHP) combination.

1. Application of Linux in servers
As the influence of open source software increases worldwide, the Linux server operating system occupies an increasing share of the entire server operating system market, and has formed a large-scale market application situation. .
With the widespread application of Linux in the server field, it has involved telecommunications, finance, government, education, transportation, agriculture, petroleum and other fields. At the same time, major hardware manufacturers have also successively supported the Linux operating system, indicating that Linux has a bright future in the server market. , it will definitely be able to impact the larger server market in the future.

2. Application of Embedded Linux
Due to the open source code, powerful functions, strong stability and great scalability of the Linux system, coupled with its extensive support for a large number of microprocessor architectures, hardware devices, graphics support and communication protocols ꎬTherefore it is also widely used in the embedded field.
At present, Linux has been widely used in mobile phones, tablet computers, routers, televisions and electronic game consoles. The Android operating system widely used on mobile devices is built on the Linux kernel. In addition, Cisco uses customized Linux in network firewalls and routers, and Alibaba Cloud has also developed a Linux-based operating system YunOS.

3. Application of desktop Linux
In recent years, Linux desktop operating system has developed very rapidly in the domestic market. System software manufacturers such as Kirin Linux, Red Flag Linux, and Deepin Linux, which won the bid, have all launched Linux desktop operating systems, which have been widely used in enterprises, OEMs (original equipment manufacturers), and governments.

PART3: Linux version selection

There are many distribution versions of Linux systems, and even its loyal users do not have much time and energy to try them all. For beginners, they need a clear direction before learning Linux. It is very important to choose one of the many versions that suits their needs. Here we will take you to understand the characteristics of each Linux version.

About the Author

The number one master in the Linux field, with over 500,000 online fans; worked for a Fortune 500 foreign company; winner of the National Graduate Scholarship; 6 years of public account writing experience, more than 500 original articles, some of which have been read more than 1 million times; Entered the short video field in 2021 and produced more than 300 short video original copywritings. Some single videos have been played more than 3.5 million times.

Insert image description here

Guess you like

Origin blog.csdn.net/dietime1943/article/details/133040340