Introduction of the command

After the installation of the server system and related services, the configuration of the system, the configuration of services, data management and the management of the operation of the server will become particularly important. Because, involves the security of the server (such as: login, operation).

Using the crontab command, you can open the CRon table for editing.

Timed task: As the name implies, a task or action is performed periodically. Like Windows, it also has a scheduled task service.

In Linux, Crond is a service used in Linux to periodically execute commands, scripts, or specified programs. Scheduled tasks are generally used in the following two ways:

  • 1. Periodically perform operations or tasks (such as log polling).
  • 2. Operations or tasks performed regularly by users, such as periodically updating and synchronizing data and backing up important data.

There are two types of task scheduling in Linux: system task scheduling and user task scheduling

System task scheduling: Periodic tasks to be performed by the system, such as writing cached data to hard disks and clearing logs. There is a crontab file in the /etc/directory, which is the configuration file of system task scheduling.

The contents of the /etc/crontab file are as follows

[root@centos7 ~]# cat /etc/crontab
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
Copy the code

According to the configuration file on the host, the system schedule task format /etc/crontab is divided into six segments separated by Spaces. The first five paragraphs are in time format. The sixth paragraph is the command to be executed. Detailed instructions:

Hour # The day # the day # the month # the month # the week # the week # the day # the day # the day # It can be any integer between 0 and 7, where 0 or 7 represents the command executed on Sunday Command #, either a system command or a script file you wrote yourselfCopy the code

The following special characters are also used in the above configuration fields

* (asterisk) # denotes all values. For example, if * is used in the first paragraph, subsequent commands are executed every minute while other conditions are met. , (comma) # Comma-separated values indicate a specified range. For example, if you use 1,3,5,7 in paragraph 4, it means 1, 3, 5, and 7. -- (center bar) # indicates a range. For example, 0-8 in the second paragraph indicates the range from 0 to 8. /(slash) # indicates the interval frequency of a time. For example, */5 is used in the first paragraph to indicate the interval of 5 minutes.Copy the code

Syntax format

crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
Copy the code

Option to show

-e # Edit the user's timer -l # List the user's timer -r # Delete the user's timer -u< user name > # Specify the user name for which the timer is to be setCopy the code

Write and configure scheduled tasks

Write a task specification

In daily production and test environments, writing scheduled tasks needs to comply with certain norms and rules. Based on my writing experience in actual production environment, the author summarizes as follows:

  • 1. When writing scheduled tasks, add comments to each line or paragraph.
  • /bin/sh. /bin/sh.
  • 3. For scheduled tasks that execute scripts, add >/dev/null 2>&1 at the end of the script to redirect unnecessary output information to null.
  • 4. When using system or service commands, use the absolute path, then write the script, and finally write the script into the scheduled task.
  • 5. Standardize the use of directories (for example, /server/cron_scripts for executing scheduled tasks).

2) Scheduled task configuration

In actual production and test environments, the configuration of scheduled tasks also requires certain operation specifications, as follows:

  • 1. For a scheduled task in the form of a command, test the execution of the command and write the command into the script.
  • 2. For scheduled tasks in the form of scripts, you need to debug the overall execution of the script first, and then use the standard directory path to write scheduled tasks.
  • 3. Perform repeated tests on scheduled tasks in the production environment and apply them to the actual production environment. Then manually check the execution status after the scheduled tasks are executed.

For details about scheduled task instances and operations, see the official documents. Readers can also write in their own experimental environment, configuration, this chapter will not do too much.

The above content comes from the new book “Linux System Operation and Maintenance Guide from entry to enterprise combat”.

Application, for example,

View scheduled tasks of the current user

[root@centos7 ~]# crontab -l
no crontab for root
Copy the code

Execute at the 5th and 15th minutes of each hour

5,15 * * * * command_name
Copy the code

5 and 15 minutes between 9 a.m. and 11 a.m

5,15 9-11 * * * command_name
Copy the code

The 5th and 15th minutes are performed every third day from 9 a.m. to 11 a.m

5,15 9-11 */3 * * command_name
Copy the code

The 5th and 15th minutes are run every Monday morning from 9 am to 11 am

5,15 9-11 * * 1 command_name
Copy the code

Restart HTTPD at 14:45 on the 1st, 11th and 21st of each month

* * /etc/init.d/ HTTPD restartCopy the code

Restart the SMB at 1:10 a.m. every Saturday and Sunday

0 /etc/init.d/ HTTPD restartCopy the code

Run the /etc/script/test. sh script every hour

01 * * * * /etc/scripts/test.sh
Copy the code

Learn a Linux command every day (96) : ntpdate

Learn one Linux command a day (97) : jobs

Recommended reading

  • Prepare for golden three silver four job-hopping season! Interview preparation and summary of interview questions have been completed (Linux operation and Maintenance)!
  • Share some technical information (architecture, database, Java, etc.), suggest collection!
  • Essential for promotion and salary increase! Operation and maintenance engineers hit strange upgrade into god’s way
  • Powerful, 10K + like SpringBoot background management system unexpectedly out of the detailed tutorial!
  • Share a set of enterprise-level open source projects based on SpringBoot and Vue, the code is very standard!
  • Can make money, open source SpringBoot mall system, super functions, super beautiful!

If you have any mistakes or other questions, please leave comments and correct them. If you have any help, please like + forward to share.