Overview
Without any manual trigger or interaction, if you want to run a specific task at a specific time you can use a scheduled task. On Linux/Unix this is done with cron / crontab; on Windows the equivalent built-in tool is Task Scheduler. This article shows a simple workflow to run a PHP script on a Windows server (WAMP) on a schedule by invoking PHP from a batch file.
Requirements
- A PHP script that is independently executable (no missing relative include paths). Example:
backupcron.php. - PHP CLI installed (php.exe) as part of WAMP (or a separate PHP install).
- Permission to create and run scheduled tasks on the Windows server.
Key points before you begin
- Use absolute paths in scripts and the batch file.
- If your PHP script depends on include files, ensure includes use absolute paths or the script sets the correct working directory.
- Replace the example PHP path below with the path on your machine (the example uses
php5.4.3as an illustration).
Procedure (step-by-step)
Create the PHP action script (example name:
backupcron.php). Make sure it is executable from the command line and does not depend on relative include paths unless handled explicitly.Create a Windows batch file that runs the PHP script. Example name:
backup.bat.Example batch file content (exact example from the original article):
@echo off
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\Test\admin\backupcron.php
Recommended safer variant (handles spaces and logs output):
@echo off
"C:\wamp\bin\php\phpX.Y.Z\php.exe" -f "C:\wamp\www\Test\admin\backupcron.php" >> "C:\wamp\www\Test\admin\backup.log" 2>&1
- Replace
phpX.Y.Zwith your installed PHP folder (e.g.,php7.4.30orphp8.1.0). - The
>>and2>&1redirect stdout and stderr to a log file for debugging.
Test the batch file from a Command Prompt. Open cmd.exe and run the batch file path explicitly. Fix any immediate PHP or path errors.
Open Task Scheduler: Start → Programs → Accessories → System Tools → Task Scheduler.

- In Task Scheduler, open the Task Scheduler Library to view scheduled jobs.

- Click "Create Basic Task" (or "Create Task" for advanced options) from the right panel and follow the wizard:
- Give the task a name and description.
- Trigger: choose the schedule (Daily, Weekly, One time, or "When I log on", etc.).

- For more control, open the Trigger properties and set repetition options (for example: every hour). Example trigger view:

- In the Action step choose to "Start a program" and set the program/script to
cmd.exeand the arguments to run your batch file. Example used in the original article:
- Program/script:
cmd - Add arguments (optional):
/c C:\wamp\www\Test\admin\backup.bat
Alternatively you can directly point the Action to the batch file:
- Program/script:
C:\wamp\www\Test\admin\backup.bat
Example action dialogs from the original article:


- Common action configuration notes:
- If using
cmd.exe /c, include the full path to your .bat file in the arguments. - If you point directly to the .bat file, set the "Start in (optional)" field to the folder containing the batch file so relative paths work.
- Use quoted paths if any path contains spaces.
- Other task options you may need to change (found on the General tab or when creating an advanced Task):
- Set the user account under which the task runs. If the task needs network access or specific privileges, use an account with the right permissions.
- Consider selecting "Run whether user is logged on or not" if you need the task to run in the background.
- If the task requires elevation, check "Run with highest privileges".
- Create or copy your batch file to the target location (example path used in the article):
C:\wamp\www\Test\admin\backup.bat
- Example batch file content (again):
@echo off
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\Test\admin\backupcron.php
backupcron.phpis the PHP action script that contains the code to generate the backup file.After creating the scheduled task, you can run it manually from Task Scheduler (right-click → Run) to validate that it works.
Verify the scheduled executions in Task Scheduler's "History" tab or by checking your output file or the log file if you redirected output in the batch file.
Troubleshooting (common failures and fixes)
- "Nothing happens" — open a command prompt and run the batch file manually to see errors.
- PHP path errors — verify the path to
php.exeand update the batch file accordingly. - Include files missing — make sure your PHP script uses absolute paths or sets
chdir(__DIR__)at the top so includes resolve relative to the script folder. - Permissions — verify the task's user account has permission to run php.exe and write to output folders. Try "Run whether user is logged on or not" and provide credentials, and/or check "Run with highest privileges" if elevation is required.
- Paths with spaces — wrap paths in double quotes.
- No error output — append
>> path\to\log.txt 2>&1to the batch file command to capture stdout/stderr.
Example logging batch file (recommended)
@echo off
"C:\wamp\bin\php\phpX.Y.Z\php.exe" -f "C:\wamp\www\Test\admin\backupcron.php" >> "C:\wamp\www\Test\admin\backup.log" 2>&1
Final checks
- Test from the command line first.
- Test running the task manually from Task Scheduler.
- Check log files and Task Scheduler History for errors.
That's all: follow the steps above to execute specific tasks at specific intervals on Windows using Task Scheduler and a simple .bat wrapper for PHP CLI.
No comments:
Post a Comment