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
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
Actions > Create Task.
STEP 3) GENERAL SETTINGS
Give your new task a name, and select “Run whether user is logged on or not”.
STEP 4) SET A TRIGGER TIME
Triggers > New > Set the script to run as desired – Once, repeat daily, weekly, or even monthly.
STEP 5) RUN PHP SCRIPT AS ACTION
Actions > 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
<?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 (RUN ONCE)
echo "php /PATH/TO/script.php" | at 1:23 PM 12/12/2012
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.
SCHEDULING WITH CRON (RUN PERIODICALLY)
sudo crontab -e
0 3 * * * /usr/bin/php /http/test.php
To repeat the script periodically, we can use the good old crontab
. 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 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, crontab -r
to stop all cron jobs.
MAC SCHEDULING
Well, macOS is Unix-based. Meaning, using the good old crontab again.
SCHEDULING WITH CRON
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 = "2077-06-05 12:34:56"; // RUN THIS AT YYYY-MM-DD HH:MM:SS
$runAT = strtotime($runAT);
// (B) LOOP UNTIL IT'S TIME
echo "Started";
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 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 not going to be happy with this “stupid dangerous endless loop alternative”. But if all else fails and this is the only one that works – Then it’s a good solution.
EXTRA BITS & LINKS
That’s all for this guide, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- Windows Schtasks – Microsoft
- Linux at command – Computer Hope
- Linux crontab examples – TecAdmin
- Simple PHP Daemons – Code Boxx
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!
Amazing article! Thanks your post