Posted by & filed under Code Tips.

This little batch file came about from needing to run a cron job from within XAMPP. Since it’s Windows I could have used a scheduled task, but I don’t have XAMPP running all the time and I wanted a little more control over when it runs.

And on this specific project, I’m working on a Moodle plugin that needs to run under cron every minute. So the program that’s running is PHP calling the Moodle CLI cron.php file. You’re probably going to want to change the script that’s called.

:loop
php /path/to/your/script.php
@timeout /t 60
@goto loop

You’ll want to change the 2nd line to whatever you want to run. Add as many lines as you want. And then change the 60 on the 3rd line to however many seconds you want it to wait. I needed it to run every minute so I’m using 60 seconds.

Now when I need to have cron active I just drop to the command line and run this batch file and leave it running until I’m done.

And for those that haven’t dealt with the command line in a while, or ever, pressing Ctrl-C will terminate the batch file.

2 Responses to “Fake cron on XAMPP”

  1. Guido

    Thanks Ryan!
    But with me I had to enter the path slashes in the other direction like this:
    php htdocs\test\sites\run.php

    Otherwise it wont find my run.php script.

    But now my run.php script includes like this:
    include (‘../includes/basic.inc.php’);

    Where now the slashes are the other way round, like its used to swith between paths in php,

    and now run.php cant find the basic.inc.php….

    Could you explain why? I´m confused… Thank you!

    Reply
    • Ryan

      I’m confused too.

      Forward slashes should be fine on Windows.

      You probably have to include the full path after php in your batch file. If you’re starting it without a slash then it’s looking relative to where it’s run from. It’s possible that it’s not running in the same folder as the actual script when it’s run as a scheduled task.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *