Home >Operation and Maintenance >Linux Operation and Maintenance >Detailed explanation of Linux scheduled task crontab

Detailed explanation of Linux scheduled task crontab

小云云
小云云Original
2018-02-26 10:45:203235browse


The crontab command is common in Unix and Unix-like operating systems and is used to set instructions to be executed periodically. Stored in the "crontab" file for later reading and execution. The word comes from the Greek chronos (χρνο), which means time. Usually, the instructions stored in crontab are activated by the daemon process. Crond often runs in the background and checks every minute to see if there are scheduled jobs that need to be executed. Such jobs are generally called cron jobs.

There is a scheduled task plan on the windows system. Through step-by-step settings, commands can be executed at a certain time. For example, PHP executes the bat file, and then the bat file executes the php file. But on Linux, you need to use crontab to do scheduled tasks. The execution of the task plan is controlled through a daemon process crond.

Create task plan

  1. Log in to the linux server

  2. Enter crontab -e. After typing, you will automatically enter the vi editor. At this time, you can write commands according to your needs, save and exit after writing.

  3. The saved file is in /var/spool/cron/

Write a scheduled plan

The time of Linux scheduled tasks has a fixed format, such as:

This task plan is to execute the test.php file every one minute. You can see that the crontab task plan is generally divided into two parts. The first part is the time, which is defined at what time, and the second part is what to do at this time. .

The PHP language is used here, the content of the test.php file

<span style="font-size: 14px;"><?php<br>    file_put_contents('/home/test.php',date('Y-m-d H:i:s',time()),FILE_APPEND);<br></span>

So this task plan is to download to home every one minute Write the time in the test.php file


There are usually five * signs in front of it and 5 times are used to represent

Minute Hour Day Month Week

##Example

You can use online tools to test whether the task plan is written correctlyhttps://tool.lu/crontab/

  1. Execute every one minute Once

    <span style="font-size: 14px;">*/1 * * * *<br></span>

  2. Executed once every day at 1.30 am

    <span style="font-size: 14px;">30 1 * * *<br></span>

  3. Executes every Monday at 8:00 am

    <span style="font-size: 14px;">0 8 * * 1<br></span>

  4. ## Every Weekdays 1-5 7:30pm, execution

    <span style="font-size: 14px;">30 19 * * 1-5<br></span>

  5. ##Every weekday 11:30am, 5:30pm Execution
  6. <span style="font-size: 14px;">30 11,17 * * 1-5<br></span>

  7. Every working day at 11:30 am, 5:40 pm Execution
  8. <span style="font-size: 14px;">30 11 * * 1-5<br>40 17 * * 1-5<br></span>
  9. Every 2 days at 6pm, execute
  10. <span style="font-size: 14px;">0 18 */2 * * <br><br></span>

  11. from above You can see the syntax of time in the chestnut:

    *, any
  1. */ N, every N time units
  2. 1-5, range
  3. 11 ,17 and
  4. number 3, time point
  5. , follow the execution script That’s it

Command

  1. ##crontab -e Write task plan

  2. ##crontab -l List all existing task plans

  3. crontab -r Delete task plan

Related recommendations:

yii2 Configure crontab scheduled tasks

What is the Linux user control scheduled task Crontab command and detailed explanation

Example explains how to use crontab to regularly back up MySQL

The above is the detailed content of Detailed explanation of Linux scheduled task crontab. 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