PHP利用服务器实现定时任务
2022-07-28 15:09:43
146
{{single.collect_count}}

利用服务器实现简单的定时任务,Windows的计划任务,Linux的cron,适用于每天某一特点时间执行

1.windows下定时访问指定url

windows下用定时任务执行auto.php文件, auto.php文件里用curl请求指定的接口实现

auto.php 代码如下

function doCurlGetRequest($timeout = 5){$url = 'http://127.0.0.1:81/index.php?s=/Admin/Index/dayBonus.html';$con = curl_init((string)$url);curl_setopt($con, CURLOPT_HEADER, false);curl_setopt($con, CURLOPT_RETURNTRANSFER,true);curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);return curl_exec($con);}$result = doCurlGetRequest();var_dump( $result);die;

 

新建bat文件命令如下

C:\phpStudy\PHPTutorial\php\php-7.2.1-nts\php.exe -q C:\zx\auto.php

php路径:C:\phpStudy\PHPTutorial\php\php-7.2.1-nts\php.exe

auto.php路径:C:\zx\auto.php

window 计划任务

2.linux下请求url

Linux上使用CronTab定时执行

执行 crontab -e

进入编辑模式增加一行

* * * * curl https://www.aaa.com/aaa.php

前面一部分是时间,后面操作内容。

30 * * * *

30就是每小时分钟数为30时,执行。

时间参数由以下几部分构成

 

分 时 日 月 周

第1列表示分钟1~59 每分钟用或者 */1表示,/n表示每n分钟,例如*/8就是每8分钟的意思

第2列表示小时0~23

第3列表示日期1~31

第4列表示月份1~12

第5列标识号星期0~6

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}