d
Amit DhamuSoftware Engineer
 

Automating PHP Using Task Scheduler

6 minute read 00000 views

PHP is a request/response server platform. Users request data from the server maybe in the form of a link on a page, PHP goes out to the server before the new page loads (i.e. server-side) and a response is then sent back to the user. This architecture means that it can be very difficult to automate tasks for your applications as you are relying on user interaction. As we can execute PHP from the command line, I'll show you how to integrate that within a program called Task Scheduler which will allow to execute a PHP file when it matches certain conditions and triggers.

Before we begin, please note that Task Scheduler is a Windows only solution. If your site runs on Linux, you will need to look at daemons and cronjobs which is out of the scope of this guide.

4 Step Walkthrough

  1. Setup a PHP Environment Variable (if required)
  2. Writing our PHP script
  3. Creating a BAT file which executes our PHP script
  4. Set up a task in Task Scheduler to execute the BAT file at certain times

1. Setup a PHP Environment Variable (if required)

You may have already done this step so skip it if you have. If you haven't, firstly go to Control Panel > System and Security > System > Advanced System Settings.

Next click on Environment Variables (shown below).

"-"

Next click on the Path under System Variables and click Edit. Then you need to add in the path to your PHP folder. In my case that would be C:\\Apache\\php.

"-"

All variables declared in the field are separated by a semi-colon (;) so do not forget this when adding in your path. After you have added your PHP path, click OK, OK then Apply and then restart your computer. Once restarted you have successfully added your environment variable for PHP on your machine.

2. Writing our PHP script

This is the easy bit. I have already written a simple PHP script that will create a new text file each day, append the date to it's filename and write a small string inside it. Nothing too complicated here. I have saved this in the root of my web projects folder but it could be located anywhere.

$today = date('Y-m-d');
$filename = 'text-file-' . $today . '.txt';

$file = fopen($filename, 'w');
fwrite($file, "Log for $today...");
fclose($file);

3. Creating a BAT file which executes our PHP script

Open up Notepad and type the following:

# automatic.bat
php C:\\Apache\\htdocs\\automatic.php

The first part indicates that we want to execute a PHP script and the second part indicates the location of where my PHP file is located. In my case as mentioned earlier, in the root of my web projects folder. Now save the document as automatic.bat in the same location as your PHP file (doesn't have to be but for simplicity's sake).

To test what we have done so far, double-click the automatic.bat file you just created and you should see a text file created in the same directory with the date appended to the file name. Now we know that this is working, we can move on to the final step.

4. Set up a task in Task Scheduler to execute the BAT file at certain times

"-"

  • Open up Task Scheduler on your machine and click Create Basic Task. You can also click Create Task also but the Basic Task gives you a wizard which is great if you've never used Task Scheduler before.
  • Give the task a name and description and click Next.
  • Now select Daily as the Task Trigger and click Next.
  • Now select a date for the task to start executing and a time you would like it to occur. We want to leave Recur Every to 1 day as once a day is enough for our needs. Now click Next.
  • We want to Start A Program when this task initiates so select this and click Next.
  • "-" Our Program/Script is our automatic.bat file we created earlier and I've added in the Start In parameter as my web projects folder. This is because when this executes; as I haven't specified a full path in my PHP script as to where my text file gets created; it will create it in the same folder that the script resides in or is executed from.
  • Click Next to summarise and then click Finish.

DONE! "-"

You have successfully setup your task.

You are able to test that your script works by right-clicking on the task in Task Scheduler and clicking Run. Similarly, you can edit the rules of the task by clicking Properties.

I am no expert using Task Scheduler or the Command Line but hopefully this will help some users wanting to automate simple PHP tasks for their applications. It must be said that you are also able to write MySQL queries in your script file so you are not limited in terms of the type of automated scripts you want.