16

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

Starx
  • 77,474
  • 47
  • 185
  • 261

10 Answers10

14

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

Community
  • 1
  • 1
Daff
  • 43,734
  • 9
  • 106
  • 120
  • 1
    Side note: The current program to use the task scheduler is `schtasks`, `at` has been deprecated for a few years as it represents only the features present in older versions of the task scheduler. – Joey May 03 '11 at 18:35
8

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.

Vaibhav V. Joshi
  • 175
  • 3
  • 14
5

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.

AsgarAli
  • 2,201
  • 1
  • 20
  • 32
  • 1
    To refine my up-vote - this is a .php file that is triggered by running in a browser. It will do the job, but keep in mind it will leave some Apache processes running. – James Bailey Aug 10 '16 at 19:10
4

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.

Ashwin
  • 450
  • 1
  • 6
  • 17
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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    i don't think this answers the question. wamp is for windows and cron is for unix/linux so just telling him to install cron does not make any sense. what he probably meant was to suggest some alternative to cron for windows – Chetan Paliwal Feb 25 '14 at 21:35
  • 1
    @ChetanPaliwal — Lots of UNIX software runs on Windows. For instance, Cron: http://cronw.sourceforge.net/ – Quentin Feb 25 '14 at 22:47
  • yes but just saying `install cron` as answer without giving any links or references,in my opinion,is (was) not a good way to answer – Chetan Paliwal Feb 25 '14 at 23:08
3

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.

Ihsanullah khan
  • 771
  • 1
  • 6
  • 10
2

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
-2

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.

ZoFreX
  • 8,812
  • 5
  • 31
  • 51
  • Well what I meant was, what is the cron job doing? In website terms "cron" is normally synonymous with "job that runs occasionally", which could be achieved with task scheduler or manually hitting it. If you literally need to test a crontab, that's a different problem. – ZoFreX Jul 13 '10 at 13:28
  • Ok, then how to use task scheduler to execute a PHP script in a certain Interval of time? – Starx Jul 22 '10 at 02:34
-2

Try this commnad

<?php

echo exec('0 13 * * * D:\wamp\bin\php -f D:\wamp\www\be.php');

?>
Joey
  • 344,408
  • 85
  • 689
  • 683
Syed Shah
  • 1
  • 1
  • I imagine that if the OP is mentioning WAMP he wants to hit a web page, not a php file. So it should run in the context of a browser, and thus use e.g. Curl. Yöu might not have the PHP web server variables like $_GET and $_SESSION. Also will exec actually run on Windows? – Michiel van der Blonk Mar 19 '22 at 00:26
-2

<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

  • 4
    This is redirection not Cron job. Cron job, perform silently. The entire concept is to be able to perform some task, without user intervention. This will open the page in the window and process. – Starx Feb 27 '12 at 08:16