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

后端 未结 10 2069
一生所求
一生所求 2020-12-31 06:06

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

相关标签:
10条回答
  • 2020-12-31 06:33

    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.

    0 讨论(0)
  • 2020-12-31 06:35

    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.

    0 讨论(0)
  • 2020-12-31 06:36

    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.

    0 讨论(0)
  • 2020-12-31 06:38

    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.

    0 讨论(0)
  • 2020-12-31 06:41

    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.

    0 讨论(0)
  • 2020-12-31 06:43

    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.

    0 讨论(0)
提交回复
热议问题