RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Writing and Automating PHP Crons in Windows and Linux : Page 3

Find out how to automate PHP scripts so they run unattended at scheduled times on both Windows and Linux.

Running PHP Crons in Windows Vista
To execute PHP scripts in Windows you have two options: You can run crons manually or use the Windows Task Scheduler service to schedule them to run at specific times and frequencies.

Again, the examples here all use the PHP script mycron.php listed earlier.

Running crons manually is not typically an effective solution, because it requires a human to physically type the command(s). You can simplify the process by creating a batch file. First, create a text file with a .bat extension (cron.bat in this case) and add two lines to it, using the following syntax:

   SET PATH="{php.exe HOME}";
   start php.exe phpscriptname.php
For example, to run the mycron.php script, you would write:

   start php.exe mycron.php
The first line sets the current directory to the location of the PHP application server executable file, and the second line starts the PHP script in a new window.

Place the PHP script, mycron.php, in the same directory as the batch file (or update the path in the batch file to point to the location of mycron.php).

Every time the operating system runs the crontab, Windows will open a MS-DOS window, run the script, and close the window when the script completes. You'll see any output in the logs subdirectory.

Your other option is to use the Task Scheduler Windows service. All versions of Windows since at least Windows 95 can use Task Scheduler to schedule jobs that run at specified times; however, Windows Vista runs the tasks more predictably and securely then older versions of Windows. The most important new features of the Task Scheduler in Vista are:

  • Multiple conditions that can restrict a task to run only when the machine is in a given state. For example, you can schedule a task to run only when the computer is in an idle state, on AC power source vs. battery power, has network connectivity, or only when the computer is "ON" versus in a sleep state.
  • Task Scheduler in Windows Vista supports triggers that can launch tasks at machine idle, startup, or logon.
  • Administrators can use settings to instruct Task Scheduler what actions to take if a task fails to run correctly. If a task fails, administrators can specify how many times to retry it. They can also set the maximum execution time for a task, which will stop the task if it runs too long.
To read more about the new Task Scheduler features of the Windows Vista and to see a comparison between the Windows Vista vs Windows XP and Windows Server 2003 versions follow this link: http://technet.microsoft.com/en-us/appcompat/aa906020.aspx.

To run a PHP script using Task Scheduler in Windows Vista follow these steps:

  1. Open Task Scheduler using Start ( All programs ( Accessories ( System Tools ( Task Scheduler.
  2. In the Task Scheduler window, choose Action ( Create Task…
  3. In the General tab of the Create Task window enter a name for the task and (optionally) a description (see Figure 5).
  4. In the Actions tab of the Create Task window, click the "New…" button (see Figure 6, top), and then select an action (see Figure 6, bottom). To run a PHP script choose the "Start a program" option.
    Figure 5. Task Scheduler: The General pane accepts a name and optional description for the task, and provides some other settings.
    Figure 6. Task Scheduler Actions Tab: To create a new action, click the New… button and select the "Start a Program" action.

  6. In the Conditions tab, specify the conditions under which you want the task to run; the task will not run until the conditions you set evaluate to true (see Figure 7).
  7. You can use the Settings tab (see Figure 8) to specify some additional settings that affect task behavior, such as what to do if the task fails or runs longer than some maximum allowable time. When you're done, click OK.
    Figure 7. Task Scheduler, Conditions Tab: Specify the condition(s) under which the task should run.
    Figure 8. Task Scheduler, Settings Tab: On this tab, you can specify additional settings for the action, such as the maximum allowable time, and the number of restart tries.

  9. Finally, in the main Task Scheduler window, select the action (named runcron here) and then press the Run button to run the PHP script (see Figure 9).
    Figure 9. Running the New Task: You can test your new task in the main Task Scheduler window by pressing the Run button (circled in the figure).

If everything worked, you should see the task result in the logs directory. That completes the process for Vista. It's a lot easier in Windows XP, as you'll see next.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date