PHP itself has no timing function, and PHP cannot be multi-threaded. PHP's scheduled task function must be combined with other tools to achieve it. For example, WordPress has built-in wp-cron function, which is very powerful. In this article, we will deeply analyze the ideas of several common PHP scheduled tasks.
Recommended related articles:
1.How to implement scheduled execution of scheduled tasks in PHP?
2.PHP timer description
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial
Let’s start with the relatively complex server execution of PHP. Once PHP is installed on the server, PHP files can be executed regardless of whether server environment software such as nginx or Apache is installed. In Linux, using the command line and CronTab to schedule tasks is an excellent choice, and it is also the most efficient choice.
First, enter the command line mode. As a server, Linux generally enters the command line mode by default. Of course, our management server also generally connects to the server remotely through tools such as putty. For convenience, we log in as the root user. After typing:
crontab -e
in the command line, a file will be opened, and it is in a non-editing state, which is the editing interface of vi. By pressing i on the keyboard to enter the editing mode, you can edit the content. Each line in this file is a scheduled task. When we create a new line, we create a new scheduled task (of course it means that this line is written in a certain format). Let's take an example now and add a line with the following content:
00 * * * * lynx -dump https://www.yourdomain.com/script.php
What does this mean? In fact, the above line consists of two parts, the first part is the time, and the latter part is the operation content. For example, in the above example,
00 * * * *
means that when the minutes of the current time are 00, the scheduled task will be executed. The time part consists of 5 time parameters, which are:
分 时 日 月 周
The first column represents minutes 1 to 59. Each minute is represented by or */1, /n represents every n minutes, for example */8 means every 8 minutes. The following is also analogous.
The second column represents hours 1 to 23 (0 represents 0 o'clock)
The third column represents dates 1 to 31
The fourth column represents month 1 ~12
The 5th column identifies the day of the week 0~6 (0 means Sunday)
The latter part of the entire sentence is the specific content of the operation.
lynx -dump https://www.yourdomain.com/script.php
means accessing this url through lynx. We mainly use lynx, curl, and wget to achieve remote access to URLs. If we want to improve efficiency, directly using PHP to execute local PHP files is the best choice, for example:
00 */2 * * * /usr/local/bin/php /home/www/script.php
This The statement can be executed through the Linux internal PHP environment at 0 minutes every 2 hours. Note that this is not accessed through the URL and executed through the server environment, but directly executed because the server environment is bypassed. Of course the efficiency is much higher.
Okay, a few required scheduled tasks have been added. Click the Esc key on the keyboard, enter ":wq" and press Enter. This saves the set scheduled task, and you can also see a prompt on the screen that a new scheduled task has been created. The next step is to write your script.php properly.
I won’t introduce more usage of CronTab here. If you want to use this scheduled task function more flexibly, you should learn more about crontab yourself.
There is a similar cmd and bat file on Windows and Linux. The bat file is similar to a shell file. Executing this bat file is equivalent to sequentially Execute the commands inside (of course, you can also implement programming through logic), so we can use the bat command file to implement PHP scheduled tasks on the windows server. In fact, the principle of scheduled tasks on Windows is the same as that on Linux, but the methods and approaches are different. Okay, let’s get started.
First, create a cron.bat file in a location that you think is more appropriate, then open it with a text editor (Notepad will work), and write the following content in it:
D:\php\php.exe -q D:\website\test.php
This sentence means that using php.exe to execute the php file test.php is the same as the contab above, bypassing the server environment and the execution efficiency is relatively high. After writing, click Save and close the editor.
The next step is to set up a scheduled task to run cron.bat. Open in sequence: "Start -> Control Panel -> Task Schedule -> Add Task Schedule", set the time and password of the scheduled task in the opened interface, and mount cron.bat by selecting it. OK, such a scheduled task has been created. Right-click on the scheduled task, run, and the scheduled task will start executing. When the time is up, cron.bat will be run for processing, and cron.bat will execute php.
如果站长没有自己的服务器,而是租用虚拟主机,就无法进入服务器系统进行上述操作。这个时候应该如何进行php定时任务呢?其实方法又有多个。
在一个php文档的开头直接来一句:
ignore_user_abort(true);
这时,通过url访问这个php的时候,即使用户把浏览器关掉(断开连接),php也会在服务器上继续执行。利用这个特性,我们可以实现非常牛的功能,也就是通过它来实现定时任务的激活,激活之后就随便它自己怎么办了,实际上就有点类似于后台任务。
而sleep(n)则是指当程序执行到这里时,暂时不往下执行,而是休息n秒钟。如果你访问这个php,就会发现页面起码要加载n秒钟。实际上,这种长时间等待的行为是比较消耗资源的,不能大量使用。
那么定时任务到底怎么实现呢?使用下面的代码即可实现:
<?php ignore_user_abort(true); set_time_limit(0); date_default_timezone_set('PRC'); // 切换到中国的时间$run_time = strtotime('+1 day'); // 定时任务第一次执行的时间是明天的这个时候$interval = 3600*12; // 每12个小时执行一次if(!file_exists(dirname(__FILE__).'/cron-run')) exit(); // 在目录下存放一个cron-run文件,如果这个文件不存在,说明已经在执行过程中了,该任务就不能再激活,执行第二次,否则这个文件被多次访问的话,服务器就要崩溃掉了do { if(!file_exists(dirname(__FILE__).'/cron-switch')) break; // 如果不存在cron-switch这个文件,就停止执行,这是一个开关的作用 $gmt_time = microtime(true); // 当前的运行时间,精确到0.0001秒 $loop = isset($loop) && $loop ? $loop : $run_time - $gmt_time; // 这里处理是为了确定还要等多久才开始第一次执行任务,$loop就是要等多久才执行的时间间隔 $loop = $loop > 0 ? $loop : 0; if(!$loop) break; // 如果循环的间隔为零,则停止 sleep($loop); // ... // 执行某些代码 // ... @unlink(dirname(__FILE__).'/cron-run'); // 这里就是通过删除cron-run来告诉程序,这个定时任务已经在执行过程中,不能再执行一个新的同样的任务 $loop = $interval; } while(true);
通过执行上面这段php代码,即可实现定时任务,直到你删除cron-switch文件,这个任务才会停止。
但是有一个问题,也就是如果用户直接访问这个php,实际上没有任何作用,页面也会停在这个地方,一直处于加载状态,有没有一种办法可以消除这种影响呢?fsockopen帮我们解决了这个问题。
fsockopen可以实现在请求访问某个文件时,不必获得返回结果就继续往下执行程序,这是和curl通常用法不一样的地方,我们在使用curl访问网页时,一定要等curl加载完网页后,才会执行curl后面的代码,虽然实际上curl也可以实现“非阻塞式”的请求,但是比fsockopen复杂的多,所以我们优先选择fsockopen,fsockopen可以在规定的时间内,比如1秒钟以内,完成对访问路径发出请求,完成之后就不管这个路径是否返回内容了,它的任务就到这里结束,可以继续往下执行程序了。利用这个特性,我们在正常的程序流中加入fsockopen,对上面我们创建的这个定时任务php的地址发出请求,即可让定时任务在后台执行。如果上面这个php的url地址是www.yourdomain.com/script.php,那么我们在编程中,可以这样:
// ...// 正常的php执行程序// ..// 远程请求(不获取内容)函数,下面可以反复使用function _sock($url) { $host = parse_url($url,PHP_URL_HOST); $port = parse_url($url,PHP_URL_PORT); $port = $port ? $port : 80; $scheme = parse_url($url,PHP_URL_SCHEME); $path = parse_url($url,PHP_URL_PATH); $query = parse_url($url,PHP_URL_QUERY); if($query) $path .= '?'.$query; if($scheme == 'https') { $host = 'ssl://'.$host; } $fp = fsockopen($host,$port,$error_code,$error_msg,1); if(!$fp) { return array('error_code' => $error_code,'error_msg' => $error_msg); } else { stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式 stream_set_timeout($fp,1);//设置超时 $header = "GET $path HTTP/1.1\r\n"; $header.="Host: $host\r\n"; $header.="Connection: close\r\n\r\n";//长连接关闭 fwrite($fp, $header); usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功 fclose($fp); return array('error_code' => 0); } } _sock('www.yourdomain.com/script.php');// ...// 继续执行其他动作// ..
把这段代码加入到某个定时任务提交结果程序中,在设置好时间后,提交,然后执行上面这个代码,就可以激活该定时任务,而且对于提交的这个用户而言,没有任何页面上的堵塞感。
但是上面使用sleep来实现定时任务,是效率很低的一种方案。我们希望不要使用这种方式来执行,这样的话就可以解决效率问题。我们借用用户访问行为来执行任务。用户对网站的访问其实是一个非常丰富的行为资源,包括搜索引擎蜘蛛对网站的访问,都可以算作这个类型。在用户访问网站时,内部加一个动作,去检查任务列表中是否存在没有被执行的任务,如果存在,就将这个任务执行。对于用户而言,利用上面所说的fsockopen,根本感觉不到自己的访问竟然还做出了这样的贡献。但是这种访问的缺点就是访问很不规律,比如你希望在凌晨2点执行某项任务,但是这个时间段非常倒霉,没有用户或任何行为到达你的网站,直到早上6点才有一个新访问。这就导致你原本打算2点执行的任务,到6点才被执行。
这里涉及到一个定时任务列表,也就是说你需要有一个列表来记录所有任务的时间、执行什么内容。一般来说,很多系统会采用数据库来记录这些任务列表,比如wordpress就是这样做的。我则利用文件读写特性,提供了托管在github上的开源项目php-cron,你可以去看看。总之,如果你想要管理多个定时任务,靠上面的单个php是无法合理布局的,必须想办法构建一个schedules列表。由于这里面的逻辑比较复杂,就不再详细阐述,我们仅停留在思路层面上。
很好玩的是,一些服务商提供了各种类型的定时任务,例如阿里云的ACE提供了单独的定时任务,你可以填写自己应用下的某个uri。百度云BCE提供了服务器监测功能,每天会按照一定的时间规律访问应用下的固定uri。类似的第三方平台上还有很多定时任务可以用。你完全可以用这些第三方定时任务作为跳板,为你的网站定时任务服务。比如说,你可以在阿里云ACE上建立一个每天凌晨2点的定时任务,执行的uri是/cron.php。然后你创建一个cron.php,里面则采用fsockopen去访问你真正要执行某些任务的网站的url,例如上面的www.yourdomain.com/script.php,而且在cron.php中还可以访问多个url。然后把cron.php上传到你的ACE上面去,让ACE的定时任务去访问/cron.php,然后让cron.php去远程请求目标网站的定时任务脚本。
php面向过程的特性使得其程序是从上往下执行的,利用这个特性,在我们使用include某个文件时,就会执行被引入的文件,知道include的文件内程序执行完之后,再往下执行。如果我们创建一个循环,再利用sleep,不断的include某个文件,使循环执行某段程序,则可以达到定时执行的目的。我们再进一步,并不是利用while(true)来实现循环,而是利用被include文件本身再include自身来实现循环,比如我们创建一个do.php,它的内容如下:
if(...) exit(); // 通过某个开关来关闭执行// ... // 执行某些程序// ... sleep($loop); // 这个$loop在include('do.php');之前赋值 include(dirname(__FILE__).'/do.php');
其实通过这种方法执行和while的思路也像。而且同样用到sleep,效率低。
PHP定时任务是一个非常有意思的东西,虽然说实话,用系统的php.exe去直接执行php文件的效率更高,但是对于很多普通站长而言,虚拟主机是无法做到直接php执行原生程序的。本文仅提供一些解决的思路,我也仅仅是在学习中,有很多问题或表述都不正确,希望你指出来;你可以通过本文的思路,开发出自己的一种解决方案,希望你能将方案发布,并与我一起探讨。
相关推荐:
The above is the detailed content of Several ideas for implementing scheduled tasks in PHP. For more information, please follow other related articles on the PHP Chinese website!