Use crontab command in Linux environment to set up scheduled periodic execution tasks [including PHP execution code]

Use crontab command in Linux environment to set up scheduled periodic execution tasks [including PHP execution code]

This article uses the crontab command in the Linux environment to set up scheduled periodic execution of tasks. Share with you for your reference, the details are as follows:

From the Linux help, the crontab command has the following parameters:

-u username: specifies the user to operate the timer
-e: edit timer (all)
-l: View timer
-r: Delete timer (delete a user's crontab file from the /var/spool/cron directory, and delete the current user by default)
-i: Delete the timer (prompt for confirmation before deletion)

Use scenario 1:

Perform some periodic statistical business operations, such as counting the performance of all salesmen and each team on the previous day, the number of people in each team, the amount of tasks, the amount of completion, etc. at 0:00 am every day, and inserting them into the database for easy viewing

Use scenario 2:

Instead of manually executing some repetitive operations, for example, when my girlfriend was waiting for the announcement of the score line for the postgraduate entrance examination, I wrote a timing program to monitor the news headlines of the postgraduate entrance examination website in real time, and at the same time connect to the SMS verification code platform. As long as the title of the news headline changes, a text message will be sent to the mobile phone.

(The following uses crontab command in scenario 2 as an example)

The jianting.php code to be executed:

<?php
  $log = "/var/www/html/log.txt"; //Log file (note to set writable permissions)
  $url = 'http://yz.chsi.com.cn/'; //URL to be monitored $info = file_get_contents($url); //Get the homepage html
  preg_match('|<h4>(.*?)<\/h4>|i',$info,$m);//Regular match headline content if($m[1] != '<a href="/sytj/tjyx/gosytj.action?entrytype=yzgr" rel="external nofollow" target="_blank">2018 National Master's Admissions Adjustment Intention Collection Service System</a>') { //The title has changed//Judge whether there is a record in the log. If it is empty, record one and send a text message or email notification $str = file_get_contents($log);
    $str = trim($str);
    $bom = pack('H*','EFBBBF');
    $str = preg_replace("/^$bom/", '', $str); //Remove BOM
    if($str == ""){
      $text = 'Listen for changes';   
      file_put_contents($log,$text,FILE_APPEND);
      //Send SMS or email operations...
    }
  }

Use the which command in Linux to view the PHP installation path:

which php

To set up a scheduled task, here is a picture stolen from the Internet (crontab format description):

crontab -e

//Execute the PHP file executed by the PHP installation path (note to set the executable permission)
For example: 5 0 * * * /usr/bin/php /var/www/html/jianting.php //Executed at 5:00 a.m. every day

An example of writing a command that executes once every 10 seconds:

* * * * * /usr/bin/php /var/www/html/jianting.php
* * * * * sleep 10; /usr/bin/php /var/www/html/jianting.php
* * * * * sleep 20; /usr/bin/php /var/www/html/jianting.php
* * * * * sleep 30; /usr/bin/php /var/www/html/jianting.php
* * * * * sleep 40; /usr/bin/php /var/www/html/jianting.php
* * * * * sleep 50; /usr/bin/php /var/www/html/jianting.php

Now check that there is a scheduled task:

crontab -l

Restart crontab service

service crond restart

I changed the server and encountered the problem of being unable to execute PHP files. The solution is:

5 0 * * * /usr/bin/curl http://www.xxx.com/jianting.php //Use curl command to access PHP file 5 0 * * * cd /var/www/html && /usr/bin/php /var/www/html/jianting.php

I hope this article will help you configure your Linux server.

You may also be interested in:
  • Solution to Linux crontab timing execution of Shell scripts when specific commands need to be executed
  • Detailed explanation and summary of the use of Linux scheduled task Crontab command
  • Detailed explanation of crontab scheduled execution command under Linux
  • Regularly execute commands and scripts on Linux (cron, crontab, anacron)
  • Detailed explanation of at and crontab commands for scheduled execution of tasks in Linux
  • Linux Crontab Start, Run and Edit Commands
  • Linux crontab command format and detailed examples (recommended)
  • Use of Linux crontab command

<<:  The implementation of event binding this in React points to three methods

>>:  Solution to the problem that mysql local login cannot use port number to log in

Recommend

A brief discussion on the definition and precautions of H tags

Judging from the results, there is no fixed patte...

Detailed explanation of Java calling ffmpeg to convert video format to flv

Detailed explanation of Java calling ffmpeg to co...

Introduction to TypeScript basic types

Table of contents 1. Basic types 2. Object Type 2...

JavaScript to implement mobile signature function

This article shares the specific code of JavaScri...

Detailed explanation of vue.js dynamic components

:is dynamic component Use v-bind:is="compone...

Full steps to create a password generator using Node.js

Table of contents 1. Preparation 2. Writing comma...

Native js to implement drop-down menu

Drop-down menus are also very common in real life...

v-for directive in vue completes list rendering

Table of contents 1. List traversal 2. The role o...

MySQL MyISAM default storage engine implementation principle

By default, the MyISAM table will generate three ...

MySQL 8.0.21 installation tutorial under Windows system (illustration and text)

Installation suggestion : Try not to use .exe for...

Vue implements automatic jump to login page when token expires

The project was tested these days, and the tester...

CSS3+Bezier curve to achieve scalable input search box effect

Without further ado, here are the renderings. The...

TypeScript decorator definition

Table of contents 1. Concept 1.1 Definition 1.2 D...

Search engine free collection of website entrances

1: Baidu website login entrance Website: http://ww...