Home  >  Article  >  Backend Development  >  Linux uses crontab to implement PHP execution plan scheduled tasks_PHP tutorial

Linux uses crontab to implement PHP execution plan scheduled tasks_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:07882browse

First, let’s talk about cron, which is a scheduled execution tool under Linux. Users other than root can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the /var/spool/cron directory and executed using the identity of the user who created them. To create a crontab entry as a user, log in as that user, and then type the crontab -e command to edit the user's crontab. This file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab file is saved according to the username and written to the file /var/spool/cron/username. The cron daemon checks the /etc/crontab file, etc/cron.d/ directory, and /var/spool/cron directory for changes every minute. If changes are found, they are loaded into memory. This way, you don't have to restart the daemon when a crontab file changes.

Install crontab:

yum install crontabs

Instructions:
/sbin/service crond start //Start the service
/sbin/service crond stop //Close the service
/sbin/service crond restart //Restart the service
/sbin/ service crond reload //Reload configuration

View crontab service status: service crond status

Manually start the crontab service: service crond start

Check whether the crontab service has been set to start at boot, execute the command: ntsysv

Add automatic startup at boot:
chkconfig –level 35 crond on

crontab command:

Function description: Set timer.

Syntax: crontab [-u ][configuration file] or crontab [-u ][-elr]

Additional explanation: cron is a resident service that provides a timer function, allowing users to execute preset instructions or programs at a specific time. As long as the user can edit the timer configuration file, the timer function can be used. The configuration file format is as follows:
Minute Hour Day Month DayOFWeek Command

Parameters:
-e Edit the timer settings for this user.
-l List the timer settings for this user.
-r Delete the timer settings for this user.
-u Specifies the user name to set the timer.

crontab format:

Basic format:

Minutes Hours Days Months Weeks Commands

*                                                    

The first column represents minutes 1 to 59. Each minute is represented by * or */1

The second column represents hours 1 to 23 (0 represents 0 o'clock)
The third column represents dates 1 to 31
The 4th column represents the month 1~12
The 5th column identifies the week 0~6 (0 means Sunday)
The 6th column represents the command to be run

Remember the meanings of several special symbols:

“*” represents a number within the value range,
“/” represents “every”,
“-” represents from a certain number to a certain numbers,
"," separates several discrete numbers

# Use the hash sign to prefix a comment

# +————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +——- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +——- day of week (0 – 7 ) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed

A few examples of crontab are as follows:

(1) First example.

30 21 * * * /etc/init.d/nginx restart

Restart nginx at 21:30 every night.

(2) The second example, which is the example tested in this tutorial

* * * * * /usr/bin/php -f /root/test.php >> test.log

Execute the /root/test.php file every minute and output the results to test.log.

After completing the above basic work, let’s take a look at how to use crontab to execute PHP scripts regularly:

(1) I created a new test.php file under /root with the following content:

Copy code The code is as follows:
            #!/usr/bin/php -q
echo date('Y-m-d H:i:s')."from http://www.phpddt.com n";
?>

Note: You can use whereis php to find the location of the php execution file.

(2) Then crontab -e writes the following shell:

Copy code The code is as follows:
* * * * * /usr/bin/php -f /root/test. php>> test.log

Note: test.php must be an executable file: chmod +x test.php

The test results are normal, the screenshot is as follows:

Linux uses crontab to implement PHP execution plan scheduled tasks_PHP tutorial

Of course you can use crontab -e to continue adding tasks. You can see a root file under /var/spool/cron.
Use Windows to schedule tasks directly under Windows, and just open the web page through bat. It is not copied like Linux.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/768125.htmlTechArticleFirst let’s talk about cron, which is a scheduled execution tool under Linux. Users other than root can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in...
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