问题
How to test a cron job in Local Server like WAMP?
回答1:
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 question recommends using the at command.
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).
回答2:
You can create a html page and open it on browser. The javascript setInterval function will call for specified periods.
Following is the code to do this. Specify your interval (5000 eg. which runs every 5sec)
<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.
回答3:
Install cron (yes, it is available for Windows).
I wouldn't want to 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.
回答4:
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
sleep($timetowait);
determine the time to wait before executing the next
loop of while()
ignore_user_abort(true);
let the script continue even if browser is closed
while(1)
is an infinite loop, so this will never stop until you exit wamp.
回答5:
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.
回答6:
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.
- 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.php
in the argument
So that's complete. There is no need for installing anything.
回答7:
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
回答8:
Try this commnad
<?php
echo exec('0 13 * * * D:\wamp\bin\php -f D:\wamp\www\be.php');
?>
回答9:
<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
回答10:
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:
0 * * * * wget http://example.org/cron.php
In which case you just need to manually hit your cron php file to simulate the behaviour.
来源:https://stackoverflow.com/questions/3237186/how-to-test-a-cron-job-in-local-server-like-wamp