【问题标题】:php script with cron-like feature on Wamp windows在 Wamp windows 上具有类似 cron 功能的 php 脚本
【发布时间】:2016-01-08 21:50:34
【问题描述】:

我需要从 Windows WAMP 服务器运行脚本。它应该像这样工作:

<?php
while(1){
    echo 'Start<br>';
    //Some magic here to wait 5 seconds
    echo 'End<br>';
}
?>

我想要的是自动工作:打印出Start -> 等待5 秒-> 打印出End -> 打印出Start -> 等待5 秒-> ....

在 php 中,Sleep(5); 不是一个选项,因为(据我所知)它将等待程序结束,然后输出StartEnd。 我不能使用Windows Task Scheduler,因为将来脚本将在 linux 服务器上运行。但我不能使用 cron,因为我在 Windows 上。所以,基本上我需要像 cron 这样的东西,但是在 windows 上的 php 和 wamp localhost 中。

所以,我的问题是:php/javascript/whatever 中是否有任何选项可以通过暂停迭代一个脚本“实时”?


Python 示例:

import time
for i in range(10):
    print('Start')
    time.sleep(5)
    print('End')

感谢您的每一个建议!

【问题讨论】:

标签: php windows cron localhost wampserver


【解决方案1】:

我猜你应该只使用windows task scheduler,但如果你真的需要php 脚本,你可以使用类似的东西:

<?php
set_time_limit(0); // no time limit to execute the script
ignore_user_abort(true); //ignore if the user closes the browser or shell session
while(1){
    echo 'Start<br>';
    sleep(5);
    echo 'End<br>';
}
?>

【讨论】:

    【解决方案2】:

    就像 Pedro 提到的你需要一个体面的任务计划,你也可以看看 @this article 来获得 windows 中的 cron 类型的功能

    【讨论】:

    • 任务计划至少和 Cron 一样有用
    猜你喜欢
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多