Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Cron Job on PHP Script, on localhost in Windows

Tags:

I have a php script and want to run it on an schedule. I am using local web server on windows (WAMP server) and need a way to run my_script.php every 10 min.

How to run a cron job on a PHP script, on localhost in windows?

like image 985
Pmpr.ir Avatar asked Jun 04 '14 10:06

Pmpr.ir


People also ask

Can I run cron job in localhost?

Cron on Windows Create your new scheduled task based on the following items: For the scheduled task's command to run, enter the path and filename for your installed web browser, followed by the address of your localhost website's cron.

How do I schedule a cron job in windows?

- Go to Start >> Control Panel >> Scheduled Tasks >> Add Scheduled Task. - Type "Moodle Cron" as the name of the task and select "Daily" as the schedule. Click "Next". - Select "12:00 AM" as the start time, perform the task "Every Day" and choose today's date as the starting date.


1 Answers

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.
like image 183
Pmpr.ir Avatar answered Sep 27 '22 17:09

Pmpr.ir