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

How to change the default character set of MySQL to utf8 on MAC

1. Check the character set of the default install...

Summary of the top ten problems of MySQL index failure

Table of contents background 1. The query conditi...

Use the CSS border-radius property to set the arc

Phenomenon: Change the div into a circle, ellipse...

Tutorial on how to deploy LNMP and enable HTTPS service

What is LNMP: Linux+Nginx+Mysql+(php-fpm,php-mysq...

A brief discussion on common operations of MySQL in cmd and python

Environment configuration 1: Install MySQL and ad...

What do CN2, GIA, CIA, BGP and IPLC mean?

What is CN2 line? CN2 stands for China Telecom Ne...

Implementation of MySQL custom list sorting by specified field

Problem Description As we all know, the SQL to so...

Detailed graphic and text instructions for installing MySQL 5.7.20 on Mac OS

Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...

Summary of 3 minor errors encountered during MySQL 8.0 installation

Preface In the past, the company used the 5.7 ser...

Summary of vue's webpack -v error solution

Xiaobai learned about Vue, then learned about web...

mysql wildcard (sql advanced filtering)

Table of contents First, let's briefly introd...

Mysql optimization techniques for querying dates based on time

For example, to query yesterday's newly regis...

Detailed explanation of the use cases of Vue listeners

The first one is to use jQuery's ajax to send...

Steps to install MySQL on Windows using a compressed archive file

Recently, I need to do a small verification exper...