Introduction to the use of crontab command in Linux

One, the role of crontab command

crontab is a command used to perform timing tasks in Linux. Installing the Linux operating system will support the use of this command by default.

⚠️Note : The newly created cron task will not be executed immediately. It will take at least 2 minutes before it can be executed immediately. You can restart cron to execute it immediately.

Two, Linux timing task classification

  • System level: the tasks that the system needs to perform periodically, such as backing up system data and clearing cache
  • User level: a job that a user needs to do on a regular basis, such as checking the mail server for new information every 10 minutes

Three, specific introduction

3.1 Syntax
crontab [ -u user ] file
// 或者
crontab [ -u user ] { -l | -r | -e }

Parameter Description:

  1. crontab: is used to allow users to perform timing tasks at a fixed time or at a fixed interval
  2. -u user: refers to setting the timed tasks of the specified user. The premise is that you must have the authority (for example, root) to be able to specify the timed tasks of others. If you do not use -u user, it means to set your own timed tasks
  3. -e: Use a text editor to set timed tasks. The default text editor is VI. If you want to use another text editor, please set the VISUAL environment variable to specify which text editor to use (for example, setenv VISUAL joe)
  4. -r: delete the current scheduled task
  5. -l: List current timed tasks
3.2 The time format is as follows:
f1 f2 f3 f4 f5 program
  • Where f1 is the minute, f2 is the hour, f3 is the day of the month, f4 is the month, and f5 is the day of the week. program represents the program to be executed.
  • When f1 is *, it means that the program must be executed every minute, when f2 is *, it means that the program must be executed every hour, and so on
  • When f1 is ab, it means that it will be executed from the a-th minute to the b-th minute, and when f2 is ab, it means that it will be executed from the a-th hour to the b-th hour, and so on
  • When f1 is */n, it means that it will be executed every n minutes, f2 is */n which means it will be executed every n hours, and so on
  • When f1 is a, b, c,..., it means that it will be executed in minutes a, b, c,..., and when f2 is a, b, c,..., it means that it will be executed in a, b, c...hours, and so on.

Note:
Users can also store all tasks to be executed in a file first, and use crontab file to set the task execution time.

Four, specific examples

Example 1:

(1) Execute and crontab -lview current existing timing tasks

crontab -l

The results are as follows:

#Ansible: Ntpdate server for sync time
*/20 * * * * /usr/sbin/ntpdate -u 172.16.120.10
*/60 * * * * echo "" > /opt/app/tomcat/apachetomcatserver/webapps/agent.log

(2) Use crontab -eedit timed tasks to add new tasks

// 第一步:打开编辑器,准备编辑新内容
crontab -e
// 第二步:添加新内容并保存
*/60 * * * * echo "" > /opt/app/tomcat/apachetomcatserver/webapps/agent.log

(3) Restart crond

systemctl reload crond
systemctl restart crond

Guess you like

Origin blog.csdn.net/sinat_34241861/article/details/112254005