» » PHP+Crontab performs scheduled tasks


PHP+Crontab performs scheduled tasks

Author: bamboo06 on 21-04-2019, 02:03, views: 1468

When we do web project development, we often need to periodically back up data, periodically restart a service or periodically execute a PHP program, etc., generally use Crontab under Linux and use scheduled tasks under Windows. This article focuses on the basics of using Crontab under Linux and performing PHP tasks.
PHP+Crontab performs scheduled tasks

Crontab is a commonly used timing execution tool for Unix/Linux systems that runs specified jobs without human intervention.

Install Crontab
yum install vixie-cron crontabs // install Crontab
chkconfig crond on // set to boot from boot
service crond start //start

Description: The vixie-cron package is the main program for cron; the crontabs package is the program used to install, uninstall, or enumerate the tables used to drive the cron daemon.

Using Crontab
Cron is a resident service that provides a timer function that allows users to execute preset commands or programs at specific times. As long as the user edits the timer's configuration file, the timer function can be used.

View crontab timing execution task list
crontab -l

Add crontab to perform tasks regularly
crontab -e

Crontab task command format
Format: minute hour dayofmonth month dayofweek command
Explanation: Minutes Hours Dates Monthly Payment Week Order
Range: 0-59 0-23 1-31 1-12 0-7, 0 and 7 are on Sunday

In the crontab we will often use the 4 symbols of *, - /n, they all have a specific meaning.
Symbol Description
* (asterisk) represents all valid values. For example: 0 23 * * * backup Execute the backup command at 23 o'clock on the day of the week.
, (comma) means splitting multiple values. Such as: 30 9 1,16,20 * * command The command command is executed at 9:30 on the 1st, 16th, and 20th of each month.
- (minus sign) represents a range of time. Such as 0 9-17 * * * checkmail Execute the checkmail command from 9:00 to 17:00 every day.
/n stands for every n long time. For example, */5 * * * * check Executes the check command every 5 minutes, the same as 0-59/5.

Give a few chestnuts:
The backup program is executed every day at 3:00 am: 0 3 * * * /root/backup.sh
The log cleanup process is performed at 8:30 every Sunday: 30 8 * * 7 /root/clear.sh
Execute the test program at 0:00 every week for 1 week: 0 0 * * 1,5 test
The wenchuan program is executed at 14 o'clock every year on May 12th: 0 14 12 5 * /root/wenchuan
Restart php-fpm every 15 minutes from 18 to 23 every night: */15 18-23 * * * /etc/init.d/php-fpm

Execute PHP scheduled tasks
Example 1: Perform business data statistics tasks at 1 o'clock every morning.
5 * * * * /usr/local/bin/php -f /home/web/stat.php >> /dev/null 2>&1

Example 2: Get the WeChat access_token at the 5th minute of each hour.
5 * * * * /usr/local/bin/php -f /home/web/access.php >> /dev/null 2>&1

/usr/local/bin/php is the installation path for your PHP executable.

The minimum granularity of a Crontab scheduled task is minutes, so what if we want to perform a second-level scheduled task? For example, do you perform a task every 10 seconds? One way is to use the bash script sleep to achieve second-level timing. Another way is to use the Swoole extension to implement the second-level task. Next we will post a special timer program that supports the second-level processing based on the swoole extension.

Tags: php, linux, cron

Category: PHP Scripts / Apps

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
Comment on the news site is possible only within (days) days from the date of publication.