Running PHP Scripts as Cron Jobs on Windows

How do I visit/get a set of web pages to run scripts that perform tasks or update data?

Is there a way to set up a cron job or at least execute a php file to run every so often?

On Windows you would use the “Windows Task Scheduler” to execute PHP scripts either via wget/curl and a URL, or directly by php.exe and a file path – at specific time intervals (or on specific events).

Run PHP Script Via URL

To visit a URL you can use command line tools such as “wget” or “curl” (wget and curl are already included in WampDeveloper Pro under the Tools folder), and for multiple URLs you would wrap it all up into 1 batch file.

For example, to visit this URL…
http://domain.name/webapp/tools/cron.php

You would use “wget” by executing this command…

C:\WampDeveloper\Tools\gnuwin32\wget.exe -q -O NUL http://domain.name/webapp/tools/cron.php

* The “-q” switch makes wget silent and the “-O NUL” option discards the output (instead of saving it to a file).

Set up a “Windows Scheduled Task” (via “Task Scheduler”)…

Create Task...
Triggers: Daily
Advanced settings... Repeat task every: 5 minutes; Indefinitely
Action: Start a program
Program/script: C:\WampDeveloper\Tools\gnuwin32\wget.exe
Arguments: -q -O NUL http://domain.name/webapp/tools/cron.php
Start In: C:\WampDeveloper\Tools\gnuwin32\

Run PHP Script Directly

An even simpler and more straight-forward way to perform the above is to just run the PHP scripts directly without involving Apache, or anything other than the PHP interpreter/engine (php.exe)…

For example, to create a Task that executes this PHP file every 5 minutes…
C:\WampDeveloper\Websites\domain.name\webroot\webapp\tools\cron.php

Set up a “Windows Scheduled Task” (via “Task Scheduler”)…

Create Task...
Triggers: Daily
Advanced settings... Repeat task every: 5 minutes; Indefinitely
Action: Start a program
Program/script: C:\WampDeveloper\Components\Php\php.exe
Arguments: -f C:\WampDeveloper\Websites\domain.name\webroot\webapp\tools\cron.php
Start In: C:\WampDeveloper\Websites\domain.name\webroot\

* The ‘start in’ path would need to reflect the path the script is expecting to be started in (so all its inner relative paths work). Usually this is either the webapp’s folder, or the folder the script is in.

You could also create this Task via the command-line –

schtasks /create /sc minute /mo 5 /tn "my task name" /tr "C:\WampDeveloper\Components\Php\php.exe -f C:\WampDeveloper\Websites\domain.name\webroot\webapp\tools\cron.php"

* The command-line method doesn’t allow a start-in directory.

Run Multiple PHP Scripts via Batch File

If you have multiple scripts or URLs to get on 1 Task, create a batch file (tasks.bat) containing multiple instances of the above commands…

C:\WampDeveloper\Tools\gnuwin32\wget.exe -q -O NUL http://domain.name/url1
C:\WampDeveloper\Tools\gnuwin32\wget.exe -q -O NUL http://domain.name/url2
C:\WampDeveloper\Tools\gnuwin32\wget.exe -q -O NUL http://domain.name/url3
C:\WampDeveloper\Components\Php\php.exe -f C:\WampDeveloper\Websites\domain.name\webroot\path\file1.php
C:\WampDeveloper\Components\Php\php.exe -f C:\WampDeveloper\Websites\domain.name\webroot\path\file2.php
C:\WampDeveloper\Components\Php\php.exe -f C:\WampDeveloper\Websites\domain.name\webroot\path\file3.php

Set up a “Windows Scheduled Task” (via “Task Scheduler”)…

Create Task...
Triggers: Daily
Advanced settings... Repeat task every: 5 minutes; Indefinitely
Action: Start a program
Program/script: C:\path\tasks.bat
Arguments:
Start In: C:\path\

Authentication with User and Password

If the URL requires authentication or authorization, use wget with these extra switches to perform HTTP Authentication:

wget -q -O NUL --user=username --password=password http://url

In some obscure situations, switch: --auth-no-challenge might need to be used.

Also, in newer versions of wget, the “--user” and “--password” switches might be replaced with “--http-user” and “--http-password“.

Debugging Issues

If wget is unable to get the URL or script, run it directly from the command line without silencing the output (i.e., don’t use -q -O NUL) to find out what the exact issue is:

wget http://url

8 thoughts on “Running PHP Scripts as Cron Jobs on Windows”

  1. Some versions of wget keep outputing this text, even when in silent/quite mode:

    SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
    syswgetrc = c:/progra~1/wget/etc/wgetrc

    To get rid of this, redirect STDERR (2) error text output to NUL…
    wget.exe -q -O NUL http://domain.name/webapp/tools/cron.php 2> NUL

    You can also redirect STDOUT (1) text output to NUL…
    wget.exe -q -O NUL http://domain.name/webapp/tools/cron.php 1> NUL 2> NUL

  2. I have a question related to one of the many possible scenario. Its quite common to have web-services in your application, used in one of the many script.
    In such case I am not sure if the web services will be executed properly, as web service request and response is handle by a server (in my case Apache), not by php interpreter.

    So how do you think the code will behave, when there is a web service request and the script is executed using cron job.

    Please help me here and correct me if I am wrong

  3. A lot of thanks. But a question, is it important, if I’m running with IIS, and not with Apache?

    My Web Pages are running in IIS Server with PHP installation.

  4. Hai, i’m fahrul.

    Whether this method have an authentication?.
    I mean, if other people known about that script and directly execute it, He is can not do it.

    Regards

    1. You can restrict the script URL to only allow access from IP 127.0.0.1 by editing the .htaccess file in its folder –

      order allow,deny
      deny from all
      allow from 127.0.0.1

      You can also edit the script and have it exit(); (at the very top) unless the URL passes a secret string such as in –

      http://www.example.com/path/cron-script-to-run.php?secret=1234567
      <?php
      if(!isset($_GET["secret"]) ||
         empty($_GET["secret""]) ||
         $_GET["secret"] !=== "1234567")
      {
        header($_SERVER["SERVER_PROTOCOL"] . ' 401 Unauthorized', true, 401);
        exit();
      }
      ?>
  5. Thanks for the article.
    For many users without Windows Task Scheduler knowledge, using third party cron job service (like easycron.com) is a good alternative.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>