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

Detailed explanation of SSH password-free login configuration under Linux

Assume there are two Linux servers A and B, and w...

Introduction to JavaScript strict mode use strict

Table of contents 1. Overview 1.1 What is strict ...

How to solve the Mysql transaction operation failure

How to solve the Mysql transaction operation fail...

MySQL master-slave configuration study notes

● I was planning to buy some cloud data to provid...

Implementation of CSS child element selection parent element

Usually a CSS selector selects from top to bottom...

MySQL slow query operation example analysis [enable, test, confirm, etc.]

This article describes the MySQL slow query opera...

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...

Four methods of using JS to determine data types

Table of contents Preface 1. typeof 2. instanceof...

Example of writing mobile H5 to invoke APP (IOS, Android)

iOS 1. URL scheme This solution is basically for ...

Simple operation of installing vi command in docker container

When using a docker container, sometimes vim is n...

Detailed explanation of Vue's ref attribute

Summarize This article ends here. I hope it can b...

Install Apple Mac OS X in VMWare12 Graphic Tutorial

1. Introduction: Because my friend wanted to lear...

Detailed process analysis of docker deployment of snail cinema system

Environmental Statement Host OS: Cetnos7.9 Minimu...

JavaScript to implement the back to top button

This article shares the specific code for JavaScr...