Run Cron Job on PHP Script, on Localhost in Windows

Run Cron Job on PHP Script, on localhost in Windows

recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.

By the way I am sharing this for anyone that is on the same thing.

You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php)

shellscript.vbs

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing

script.bat

"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"

Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:

  1. Open Task Scheduler from windows Start menu
  2. Go to Action menu and hit Create Task...
  3. in General tab, fill the Name and Description fields as you want
  4. in Triggers tab, hit New button.
  5. from Begin the Task dropdown, select On a schedule and choose Daily
  6. from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
  7. on Actions tab, from Action dropdown, select Start a program.
  8. on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
  9. leave Add argumentts (optional) section empty.
  10. in Start in (optional) box, enter parent directory of shellscript.vbs like C:\path\to\.
  11. Hit upvote on this tutorial :) Have fun.

How to use cron job on localhost in Windows with Laravel 5

It doesn't work because php.exe is supposed to execute a PHP file. When you point it to a URL it doesn't know what to do.

What you need is something like curl for Windows and then you can make a HTTP request:

curl http://localhost/test-cron-job

Run a php script on localhost using cron job

This is not an answer, but only pointers you need to check before you proceed further.

  1. Crons are available only on unix systems, not windows.

  2. While debugging cron or anything similar, schedule them for every minute or (any such acceptable smaller frequency), and once they work every minute, only then schedule them for the original frequencies. That will save you some time during debugging.

  3. Its an incorrect call to exec. You don't have to pass all those * in it

  4. Since this is windows, You want to do php.exe and not C:\BitNami\wampstack-5.4.21-0\php -f which would have been done in linux (with proper path)

  5. Your .bat file is currently scheduled daily, you need to schedule it every half hour.

php script with cron-like feature on Wamp windows

I gues you should just use windows task scheduler, but if you really need a php script, you can use something like:

<?php
set_time_limit(0); // no time limit to execute the script
ignore_user_abort(true); //ignore if the user closes the browser or shell session
while(1){
echo 'Start<br>';
sleep(5);
echo 'End<br>';
}
?>

can i configure cron job for localhost

Cron is a time-based job scheduler in Unix-like computer operating systems. Which means you can not configure it on Windows.

Creating A Scheduled Task on PHP Running Under IIS

You could use Task Scheduler on the server to run a batch file that runs the PHP script via command line at a regular interval, which i hope should solve your problem.



Related Topics



Leave a reply



Submit