Schedule A PHP Script to Run At Specified Time (Windows Linux Mac)

Welcome to a tutorial on how to schedule a PHP script to run at a specific time. Need to send out emails at a set time? Or maybe to run an automated update script? Yes, we can schedule PHP scripts to run at a specified time on the various platforms using their respective schedulers. Read on to find out how!

 

TABLE OF CONTENTS

Windows Scheduling Linux Scheduling Mac Scheduling
Platform Independent Useful Bits & Links The End

 

 

WINDOWS SCHEDULING

In this section, we will look at how to schedule a PHP script using the Task Scheduler in Windows.

 

WINDOWS TASK SCHEDULER

STEP 1) OPEN TASK SCHEDULER

Start > Task Scheduler.

 

STEP 2) CREATE TASK

Click on “Create Task” under “Actions”.

 

STEP 3) GENERAL SETTINGS

Give your new task a name > Select “Run whether user is logged on or not”.

 

STEP 4) SET A TRIGGER TIME

Under the “Triggers” tab > New > Set the script to run once, repeat daily, weekly, or even monthly.

 

STEP 5) RUN PHP SCRIPT AS ACTION

Under the “Actions” tab > New > Point the “Program/script” to php.exe > Add the script that you want to run under the arguments > OK.

Done. Your PHP script is now set to run on a schedule.

 

 

ALTERNATIVE – SCHEDULING WITH COMMAND LINE

If you need to schedule to run a script programmatically, try the command line schtasks:

<?php
/*
schtasks
/Create
/TN Name
/TR "D:\xampp\php\php.exe D:\http\SCRIPT.php"
/SC ONCE
/ST 12:34
/SD 11/11/2011
*/
pclose(popen('schtasks /Create /TN test /TR "D:\xampp\php\php.exe D:\http\SCRIPT.php" /SC ONCE /ST 12:34 /SD 11/11/2011', "r"));

If you need to schedule to run a script programmatically, try the command line schtasks.

  • TN Task name.
  • TR Task run.
  • SC Schedule (MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONLOGON, ONIDLE, ONEVENT).
  • ST Start time.
  • SD Start date.

 

LINUX SCHEDULING

Moving on, let us look at how to schedule a PHP script in Linux.

 

SCHEDULING WITH AT

Linux comes in various different builds and flavors, some may offer a graphical interface to schedule tasks while some don’t. So we will go straight into the most reliable way – Using the command line. To schedule a task to run once only, we use the at command.

Linux at command
echo "php /PATH/TO/script.php" | at 1:23 PM 12/12/2012

 

SCHEDULING WITH CRON

To repeat the script periodically, we can use the good old crontab:

Linux crontab
sudo crontab -e
0 3 * * * /usr/bin/php /http/test.php

The format of crontab goes like this:

  • Minute – 0 to 59
  • Hour – 0 to 23
  • Day of month – 1 to 31
  • Month of year – Either 1 to 12, or Jan to Dec
  • Day of week – 0 to 6, or Sun to Sat
  • Command to run

So for the above example, the PHP script is set to run at 3 AM every day in the morning. If you need more crontab examples, I will leave a link in the extras section below.

P.S. You can also use crontab -l to list all the existing scheduled tasks.

 

 

MAC SCHEDULING

Well, macOS is Unix-based. Meaning, using the good old crontab again.

 

SCHEDULING WITH CRON

macOS crontab
crontab -e
30 5 * * 1 /usr/bin/php /http/test.php

Yes,  it’s the same as Linux. But here is just another example that will run at 5:30 AM every Monday.

 

SCHEDULING WITH GUI

For you guys who want to use the GUI to schedule a task, it’s a painful process the last I heard – It basically involves:

  • Using Automator to create an “auto-run PHP shell script” workflow.
  • Then use iCal to schedule run the Automator task.

I am not a Mac guru, so do your own research… Crontab is just a lot easier though.

 

PLATFORM INDEPENDENT SCHEDULING

So what do we do when we have no access to a task scheduler? Here is a final alternative.

 

INFINITE WHILE LOOP “SCHEDULER”

<?php
// (A) SET THE TIME TO RUN
$runAT = "2019-05-19 20:20:20"; // RUN THIS AT YYYY-MM-DD HH:MM:SS
$runAT = strtotime($runAT);
echo "Started";
 
// (B) LOOP UNTIL IT'S TIME
while (true) {
  if (strtotime("now") >= $runAT) {
    // (B1) DO YOUR STUFF HERE
    echo "Running!";
 
    // (B2) STOP LOOPING
    break;
  }
  sleep(10); // WAIT FOR X SECONDS
}

Simply run this script in the command line or use exec("PHP FOLDER/SCHEDULER.PHP") to launch it – This script will simply keep looping until it hits the preset $runAT to run whatever is required.

P.S. Beware though, many instances of this script can be spawned. You might want to create some kind of “locking mechanism” to prevent too many from running.

P.P.S. Some “master code trolls” are probably not going to be happy with this “stupid alternative”. But if all else fails and there is an alternative that actually works – Then it’s a good solution.

 

 

USEFUL BITS & LINKS

That’s all for this project, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

1 thought on “Schedule A PHP Script to Run At Specified Time (Windows Linux Mac)”

Leave a Comment

Your email address will not be published. Required fields are marked *