How to test a cron job in Local Server like WAMP?

51,443

Solution 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).

Solution 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.

Solution 3

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.

Solution 4

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.

Solution 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.

Share:
51,443
Starx
Author by

Starx

Website | Careers | GitHub | Freelancer | Odesk | Facebook | Google+ | Twitter | YouTube | Blog Stackoverflow* Top member from Nepal: 2011 &amp; as of March 2012 1st User from Nepal to reach 15K+, 20K+, 25K+, 30K+ on Stackoverflow to get Silver Badge in php (182th world wide), jquery (134th world wide) Projects: jQuery Fancy Menu [git] jQuery Tiny Highlighter [git] Stackexchange

Updated on May 08, 2020

Comments

  • Starx
    Starx almost 4 years

    How to test a cron job in Local Server like WAMP?