php 如何在 WAMP 等本地服务器中测试 cron 作业?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3237186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test a cron job in Local Server like WAMP?
提问by Starx
How to test a cron job in Local Server like WAMP?
如何在 WAMP 等本地服务器中测试 cron 作业?
采纳答案by Daff
Windows doesn't have Cron (it is the main task scheduling program for Linux systems). The Windows version for that is the Task Scheduler. This questionrecommends using the at command.
Windows 没有 Cron(它是 Linux 系统的主要任务调度程序)。Windows 版本是Task Scheduler。这个问题建议使用 at 命令。
So that Cron doesn't have anything to do with the Apache, Mysql, PHP setup I don't think it is possible to reliably test the cronjobs you created for the Linux Cron in windows (maybe with Cygwin).
因此 Cron 与 Apache、Mysql、PHP 设置没有任何关系,我认为无法可靠地测试您在 Windows 中为 Linux Cron 创建的 cronjobs(可能使用Cygwin)。
回答by Vaibhav V. Joshi
You can create a html page and open it on browser. The javascript setInterval function will call for specified periods.
您可以创建一个 html 页面并在浏览器上打开它。javascript setInterval 函数将调用指定的时间段。
Following is the code to do this. Specify your interval (5000 eg. which runs every 5sec)
以下是执行此操作的代码。指定您的时间间隔(例如 5000,每 5 秒运行一次)
<html>
<head>
<title>Cron</title>
</head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<body>
<h1>Cron page</h1>
<script type="text/javascript">
setInterval(function(){
$.get('http://localhost/test/test.php', function(data) {
console.log(data);
});
}, 5000);
</script>
</body>
</html>
Note: To avoid CORS you should call ajax from same host or allow CORS from server side.
注意:为了避免 CORS,您应该从同一主机调用 ajax 或从服务器端允许 CORS。
回答by Quentin
Install cron (yes, it is available for Windows).
安装 cron(是的,它适用于 Windows)。
I wouldn't wantto do that on Windows though. You'd probably be better off grabbing a copy of VirtualBox and creating something that better resembles your production environment to do your development in.
不过,我不想在 Windows 上这样做。您最好获取 VirtualBox 的副本并创建更类似于您的生产环境的东西来进行开发。
回答by marino andriamialy
You can run this:
你可以运行这个:
set_time_limit(0);
ignore_user_abort(true);
while (1)
{
//your code here....
sleep($timetowait);
}
You can close your browser the script will continue
您可以关闭浏览器脚本将继续
set_time_limit(0);make your script work with no time limitation
set_time_limit(0);让您的脚本不受时间限制地工作
sleep($timetowait);determine the time to wait before executing the next
loop of while()
sleep($timetowait);确定在执行下一个 while() 循环之前等待的时间
ignore_user_abort(true);let the script continue even if browser is closed
ignore_user_abort(true);即使浏览器关闭也让脚本继续
while(1)is an infinite loop, so this will never stop until you exit wamp.
while(1)是一个无限循环,因此在您退出 wamp 之前这将永远不会停止。
回答by Ashwin
you can run your script directly from URL, means if you want to run cron_test.php script from cron setting and you want to test the result for the same then you can directly run this file from localhost like http://localhost/XXXX/cron_test.php.
你可以直接从 URL 运行你的脚本,这意味着如果你想从 cron 设置运行 cron_test.php 脚本并且你想测试结果,那么你可以直接从 localhost 运行这个文件,比如http://localhost/XXXX/ cron_test.php。
回答by Ihsanullah khan
You can just cron your jobs in windows environment with just one line. I have almost spent my 5 hours so i want to share with other is make a task.
您只需使用一行就可以在 Windows 环境中 cron 您的作业。我几乎花了我的 5 个小时,所以我想与其他人分享是做一个任务。
- In program give php.exe path, with my installation it is c:\wamp\bin\php\php5.3.5\php.exe.
- Second you have to put the file absolute path, which you want to run.
-f c:\wamp\www\foo\foo.phpin the argument
- 在程序中给出 php.exe 路径,我的安装是 c:\wamp\bin\php\php5.3.5\php.exe。
- 其次,您必须放置要运行的文件绝对路径。
-f c:\wamp\www\foo\foo.php在论证中
So that's complete. There is no need for installing anything.
这样就完成了。无需安装任何东西。
回答by Mark Baker
Simply run the job from the command line. It is the job that you're wanting to test, not cron itself. If you need to have it execute at periodic intervals to simulate cron, then use the Windows "Scheduled Tasks" facility
只需从命令行运行作业。这是您要测试的工作,而不是 cron 本身。如果您需要定期执行以模拟 cron,请使用 Windows“计划任务”工具
回答by Syed Shah
Try this commnad
试试这个命令
<?php
echo exec('0 13 * * * D:\wamp\bin\php -f D:\wamp\www\be.php');
?>
回答by Nitin Handa
<meta http-equiv="refresh" content="0; url=http://localhost/myproject/cron.php" />
<meta http-equiv="refresh" content="0; url=http://localhost/myproject/cron.php" />
set up a meta referesh immediately: content = 0 every 5 seconds: content = 5
立即设置元引用:每 5 秒内容 = 0:内容 = 5
回答by ZoFreX
What do you mean by "a cron job"? On a lot of websites there is a special page like "cron.php" which is hit periodically, normally like so:
“cron 工作”是什么意思?在很多网站上都有一个特殊的页面,比如“cron.php”,它会定期被点击,通常是这样的:
0 * * * * wget http://example.org/cron.php
In which case you just need to manually hit your cron php file to simulate the behaviour.
在这种情况下,您只需要手动点击您的 cron php 文件来模拟行为。

