php - 如何连续运行 PHP 脚本?

标签 php ajax wordpress curl refresh

简单来说: 我正在编写发送和接收短信的 php 脚本, 脚本将根据每个用户注册日期计算每周向用户发送事件短信,例如每周一上午 10 点向先生发送短信。 A和每周五晚上7点发短信给B小姐.. 和 php 脚本将处理所需的一切..

问题:很明显,一个非常有趣的方法是让某人每隔几秒左右刷新我的应用程序的主页,以便能够继续计算和理解什么以及何时做工作,或者让主页始终在我的计算机上打开,以便 javascripts 和 jquery 将处理其余部分!

我的问题:我怎样才能让我的 php 程序或脚本在不需要有人刷新或打开主页的情况下保持清醒?我所说的清醒是指它感知到下一个时间表并执行它等等......

要回答的一些原始想法:也许我可以每 10 秒使用 ajax 或 curl 调用主页..但我首先不知道如何唤醒 ajax 或 curl ..

我看到一些互联网帖子建议在 linux unix 或 windows 中使用命令行之类的东西..但我通常访问主机而不是命令行,对吗?或者命令行是主机中的东西,我不知道,如果是这样请帮助我..

重要示例:有一些 php wp 插件,如 total cache 和 supper cache,它们似乎总是处于开启状态,并且不需要有人刷新页面就可以唤醒日程表..

如果可能的话,请在 php 和 php 系列中给出答案,我根本不知道 unix 或那些类型的编程..

------ 根据答案,问题取得了一些进展.. 现在我有这个波纹管脚本:

ignore_user_abort(true);
set_time_limit(0);
$data = file_get_contents('filename.txt');
$data = $data+1;
file_put_contents('filename.txt', $data);
$page = $_SERVER['PHP_SELF'];
$sec = "4";
header("Refresh: $sec; url=$page");

有效!即使我重新启动本地主机。现在的主要问题是,当我关闭主页时,它停止在 filename.txt 中递增,并且当重新添加页面时,运行递增的两个实例继续如此: 即使我关闭页面,它不应该继续增加吗? 以及我如何阻止它? 在后台运行多个页面实例是否正常?

最后:根据此页面上的说明,我最好创建一个起始页或重新加载页面,然后使用命令启动此重新加载页面,例如每 1 分钟一次,然后像平常一样编写 PHP ..

最后一点:如何停止这个后台脚本?用于更新或维护..

最佳答案

针对这个特定问题,发明了 cron 作业。 Cron 作业是定时作业,例如可以执行 PHP 脚本。

您可以设置一个 cron 作业来检查哪个用户应该每小时收到他/她的短信。根据您的操作系统,您可以设置这些 cron 作业。对于 Linux 发行版,有大量关于如何进行设置的指南。

来自 Wikipedia :

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name cron is from the Greek word for time, χρόνος (chronos). (Ken Thompson, author of cron, has confirmed this in a private communication with Brian Kernighan.)

我添加了解释如何使用 cron 作业的资源。

另一种方法是让 PHP 脚本在后台运行:

// Keep executing even if you close your browser
ignore_user_abort(true);

// Execute for an unlimited timespan
set_time_limit(0);

// Loop infinitely
// If you create a file called stop.txt,
// The script will stop executing
while (!file_exists('stop.txt')) {
    // Retrieve user data and sens sms messages

    // Wait for an hour
    sleep(3600);
}

更新

ignore_user_abort(true);
set_time_limit(0);
$data = file_get_contents('filename.txt');
while (!file_exists('stop.txt')) {
    // Add 1 to $data
    $data = $data+1;
    // Update file
    file_put_contents('filename.txt', $data);

    // Wait 4 seconds
    sleep(4);
}

要停止执行创建一个名为 stop.txt 的文件

资源

关于php - 如何连续运行 PHP 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465156/

相关文章:

php - 您如何自定义/设置 codeigniter 错误的样式?

php - <选择> 和 $_POST

php 图像类型 blob 不会显示

javascript - 如何使用ajax将js数组发送到servlet?

jQuery CORS 内容类型选项

php - 有条件必填的 WooCommerce 结帐字段

php - "Your PHP installation appears to be missing the MySQL extension which is required by WordPress."

php - PhpMyAdmin 下的同步选项卡在哪里

javascript - 我可以使用 AJAX 加载新内容,但更改多功能栏中的 URL 吗?

在 PHPMyAdmin 中导入 Wordpress DB 时 MySQL 错误 1064