How to Run a Cron Job for a PHP Script on Windows Localhost
Running my_script.php every ten minutes on a Windows machine using WAMP is fundamentally different from scheduling tasks on Linux. On Unix-like systems, you simply drop a file into /etc/cron.d or use a systemd timer. Windows does not have a native cron daemon that monitors specific files for execution. Instead, you must rely on the operating system's Task Scheduler to manage periodic PHP invocations.
The core challenge lies in how WAMP serves your application versus how the scheduler invokes it. A standard HTTP request via http://localhost/my_script.php triggers Apache, which then loads the PHP interpreter. This works fine for manual execution or web-based access, but relying on the web server loop to run a script every ten minutes introduces unnecessary load and potential locking issues if the script writes to shared resources like session files or databases.
To execute your script efficiently, you should invoke the PHP CLI (Command Line Interface) directly rather than hitting the Apache entry point. WAMP installs the CLI binary alongside the web server binaries, typically located in C:\wamp\bin\php\phpX.X.X. Using the CLI bypasses Apache entirely, ensuring that your scheduled task does not hold open connections to the web server or consume Apache's worker processes.
You can verify which PHP executable is available by opening Command Prompt and running:
"C:\wamp\bin\php\php8.x\x64\php.exe" -v
If this returns version information, you have the correct binary path. If WAMP was installed in a non-standard directory, update the path accordingly before proceeding.
Configuring Windows Task Scheduler
The most robust method is to create a scheduled task that runs your script via the CLI binary. This approach ensures reliability and isolation from the web server.
- Open Task Scheduler by typing it into the Start menu search bar.
- Click Create Task... (not "Create Basic Task," as you need advanced settings).
- In the General tab, name the task (e.g.,
MyPHPJob) and select Run with highest privileges if your script needs to access protected resources like database files or system directories. - Switch to the Triggers tab. Click New... and set the schedule to repeat every 10 minutes. Ensure "Start the task on an idle basis" is unchecked unless you specifically want it to run only when the PC sleeps.
- In the Actions tab, click New.... Set the action to Start a program.
- For the Program/script field, enter the full path to your PHP CLI binary:
"C:\wamp\bin\php\php8.x\x64\php.exe" - In the Add arguments field, provide the relative or absolute path to your script. If
my_script.phpis in your WAMP's public folder (e.g.,www), you might use:
Alternatively, if you are running from a different working directory, pass the path as an argument or ensure the script handles its own path resolution."C:\wamp\www\my_script.php"
Handling Output and Errors
One common pitfall when automating PHP scripts on Windows is silent failure. Unlike a browser where you see an error page immediately, a task scheduler runs your script in the background. If my_script.php encounters a fatal error (like a missing database connection), it will crash without logging anything visible to you unless configured otherwise.
To debug these jobs effectively, append php://stdout or redirect output to a file within your script logic, or configure the Task Scheduler action to run with an environment variable that captures output. However, a simpler approach is to ensure your PHP script explicitly logs errors to a file using error_log(). For example:
// At the top of my_script.php
ini_set('display_errors', 0); // Hide errors from CLI if not desired
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'C:\wamp\logs\my_script.log');
This ensures that every execution, successful or failed, generates a log entry you can inspect.
Alternative: Using WAMP's Built-in Scheduler
If managing Windows Task Scheduler feels too manual for your workflow, some developers prefer using WAMP's internal mechanisms, though they are less flexible for custom intervals like "every 10 minutes" without external tools. Some configurations allow setting up a loop within the script itself using sleep() and checkdate(), but this is inefficient for production-grade logic because it consumes CPU cycles waiting for time to pass rather than letting the OS handle the timing.
Final Recommendation
For a WAMP setup, the cleanest separation of concerns is keeping your scheduling logic entirely on Windows Task Scheduler while your PHP script remains agnostic of when it runs. By invoking php.exe directly from the command line, you avoid Apache overhead and ensure that your periodic tasks execute reliably regardless of web server configuration changes.