如何连续 运行 一个 PHP 脚本?
How to run a PHP script continuously?
简单来说:
我正在编写 php 发送和接收短信的脚本,
脚本将根据每个用户注册日期计算每周向用户发送活动短信,例如每周一上午 10 点向先生发送短信。 A和每周五晚上7点发短信给B小姐..
php 脚本将处理所有需要的事情..
问题:很明显,一个非常有趣的方法是让某人每隔几秒左右刷新一次我的应用程序的主页,以便能够继续计算和理解什么以及什么时候做工作,或者让主页始终在我的电脑上打开,这样 javascripts 和 jquery 就会处理剩下的事情!
我的问题:如何让我的 php 程序或脚本在不需要有人刷新或打开主页的情况下保持清醒状态?清醒是指它感知下一个时间表并执行它等等..
一些原始想法要回答:也许我可以使用 ajax 调用主页或每 10 秒卷曲一次..但我不知道如何唤醒ajax 或卷曲第一..
我看到一些互联网帖子建议在 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 中递增,并且当重新添加页面时 运行 继续递增的第二个实例:
即使我关闭页面,它不应该继续增加吗?
以及我如何阻止它?
并且在后台有多个页面实例 运行 是否正常?
finally :根据此页面上的说明,我最好创建一个启动或重新加载页面,然后使用命令启动此重新加载页面,例如每 1 分钟一次,然后编写 PHP 之类的正常..
最后一点:如何停止这个后台脚本?更新或维护..
针对这个特定问题,发明了 cron 作业。 Cron 作业是定时作业,例如可以执行 PHP 脚本。
您可以设置一个 cron 作业来检查哪个用户应该每小时收到 his/her 短信。根据您的操作系统,您可以设置这些 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
的文件
资源
您可以在几乎所有服务器中创建 cron 作业,而无需访问命令提示符。
Cron 作业可用于在指定的时间间隔(如每分钟、每小时等)在 cli 中初始化 php 脚本
简单来说: 我正在编写 php 发送和接收短信的脚本, 脚本将根据每个用户注册日期计算每周向用户发送活动短信,例如每周一上午 10 点向先生发送短信。 A和每周五晚上7点发短信给B小姐.. php 脚本将处理所有需要的事情..
问题:很明显,一个非常有趣的方法是让某人每隔几秒左右刷新一次我的应用程序的主页,以便能够继续计算和理解什么以及什么时候做工作,或者让主页始终在我的电脑上打开,这样 javascripts 和 jquery 就会处理剩下的事情!
我的问题:如何让我的 php 程序或脚本在不需要有人刷新或打开主页的情况下保持清醒状态?清醒是指它感知下一个时间表并执行它等等..
一些原始想法要回答:也许我可以使用 ajax 调用主页或每 10 秒卷曲一次..但我不知道如何唤醒ajax 或卷曲第一..
我看到一些互联网帖子建议在 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 中递增,并且当重新添加页面时 运行 继续递增的第二个实例: 即使我关闭页面,它不应该继续增加吗? 以及我如何阻止它? 并且在后台有多个页面实例 运行 是否正常?
finally :根据此页面上的说明,我最好创建一个启动或重新加载页面,然后使用命令启动此重新加载页面,例如每 1 分钟一次,然后编写 PHP 之类的正常..
最后一点:如何停止这个后台脚本?更新或维护..
针对这个特定问题,发明了 cron 作业。 Cron 作业是定时作业,例如可以执行 PHP 脚本。
您可以设置一个 cron 作业来检查哪个用户应该每小时收到 his/her 短信。根据您的操作系统,您可以设置这些 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
的文件资源
您可以在几乎所有服务器中创建 cron 作业,而无需访问命令提示符。
Cron 作业可用于在指定的时间间隔(如每分钟、每小时等)在 cli 中初始化 php 脚本