Is crontab built-in with linux?

青灯夜游
青灯夜游Original
2022-06-17 12:15:482080browse

Yes, crontab is a scheduled task tool that comes with the Linux system. Users can use the crontab tool to customize their own scheduled tasks. Through the crontab command, users can execute specified system commands or shell scripts at fixed intervals, with the syntax "crontab [-u user] file" or "crontab [-u user] [-e|-l|-r]"; The unit of time interval can be minutes, hours, days, months, weeks, or any combination of above.

Is crontab built-in with linux?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

The Linux system is controlled by the cron (crond) system service. There are a lot of planned tasks on the Linux system, so this system service is started by default. In addition, since users can also set scheduled tasks themselves, the Linux system also provides commands for users to control scheduled tasks: the crontab command. )

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.

  • User task scheduling:

    Tasks that users need to perform regularly, such as user data backup, regular email reminders, etc. 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. Its file name is consistent with the user name.

#crontab is a scheduled task tool that comes with the Linux system.

Detailed explanation of crontab command

1. Command format:

crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]

2. Command function:

Through the crontab command, we can execute specified system commands or shell scripts at fixed intervals. The unit of time interval can be minutes, hours, days, months, weeks, or any combination of above. This command is very suitable for periodic log analysis or data backup and other tasks.

3. Command parameters:

-u user:用来设定某个用户的crontab服务,例如,“-u ixdba”表示设定ixdba用户的crontab服务,此参数一般有root用户来运行。

file:file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。如果在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。

-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。

-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。

-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。

-i:在删除用户的crontab文件时给确认提示。

4. Crontab file format

Is crontab built-in with linux?

The above picture is the file format description of crontab

  • 1st column minutes 0~59

  • 2nd column hours 0~23

  • 3rd column Day 1~31

  • 4th column month 1~12

  • ##5th column week 0~7 (0 and 7 represent Sunday)

  • The command to be run in column 6

crontab common method
crontab -l #查看任务
crontab -e #编辑任务
键入 a 进入编辑模式,进行编辑后
Ctrl+c #退出编辑模式
Shift + w + q #退出编辑
wq 保存并退出

1 .Create scheduled tasks

Each user can schedule his or her own tasks and create scheduled tasks under the jingkong user.

Function: Execute once every minute, write the time to the specified file

[jingkong@muguangjingkong ~]$ crontab -e
### first crontab
*/1 * * * * /bin/date >> /home/jingkong/jk-log.txt
    List all current scheduled tasks
[jingkong@muguangjingkong ~]$ crontab -l
### first crontab
*/1 * * * * /bin/date >> /home/jingkong/jk-log.txt
    Delete all scheduled tasks
[jingkong@muguangjingkong ~]$ crontab -r

2.crontab syntax description

Enter the vi editing interface to write the tasks we want to schedule , the format of the crontab scheduling command is as follows:

* * * * * command path  //
The first five fields can take integer values ​​to specify when to start working,

The sixth field is a string, that is, the command field, which includes the crontab scheduling Command executed. Each field is separated by spaces and tabs.

*       *       *       *       *       
分:1-59,每十分钟*/10
       时: 0-23, */2 
                日: 1 -31
                        月: 1-12
                                星期: (0-6)

Example:
  • ##每天21:30 执行
    30 21 * * * cmd01
    
    ###每个月1,11,21的2:30执行
    30 2 1,11,21 * * cmd02
    
    ##每周六或者每周日,1:45执行
    45 1 * * 6,0 cmd03
    
    #每天20:00至23:00日 ,每半个小时执行一次
    0,30 20-23 * * * cmd04
    
    ##每一小时执行一次
    * */1 * * * cmd05
  • Related recommendations: "
Linux Video Tutorial

"

The above is the detailed content of Is crontab built-in with linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn