How to test a cron job in Local Server like WAMP?
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.
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.
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.
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.
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.
-f c:\wamp\www\foo\foo.php
in the argumentSo that's complete. There is no need for installing anything.
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.