Home > Article > Backend Development > Linux uses crontab to implement PHP execution plan scheduled tasks_PHP tutorial
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
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
crontab format:
Basic format:
Minutes Hours Days Months Weeks Commands
*
The first column represents minutes 1 to 59. Each minute is represented by * or */1The 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
“*” represents a number within the value range,
“/” represents “every”,
“-” represents from a certain number to a certain numbers,
"," separates several discrete numbers
# +————- 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
(1) First example.
30 21 * * * /etc/init.d/nginx restart
Restart nginx at 21:30 every night.
* * * * * /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:
(2) Then crontab -e writes the following shell:
Note: test.php must be an executable file: chmod +x test.php
The test results are normal, the screenshot is as follows:
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.