30

I have installed localhost/server in my machine and I need to run a php script using windows schedule task. how do I add path in "Actions" tab in schedule task / cofigure the script to run for particular period?

hakre
  • 178,314
  • 47
  • 389
  • 754
aron n
  • 539
  • 3
  • 9
  • 18
  • possible duplicate of [How to run a PHP file in a scheduled task (Windows Task Scheduler)](http://stackoverflow.com/questions/295386/how-to-run-a-php-file-in-a-scheduled-task-windows-task-scheduler) – MaxiWheat Mar 27 '14 at 15:43
  • You can find your answer here - http://stackoverflow.com/questions/39805601/how-to-make-a-schedulercrone-in-php-on-windows-server/39805614#39805614 – Sagar Jain Oct 01 '16 at 11:28

12 Answers12

56

Locate the php.exe executable on your system and pass it the name of the script file using the -f parameter.

Example:

C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php

Reference:

Pekka
  • 418,526
  • 129
  • 929
  • 1,058
17

Here's how did it.

Windows scheduler -> create a new task -> action tab -> Edit

enter image description here

10

At least I tried out some suggestions but it doesn't work so I tried this.

Use a bat file and schedule to execute that bat file.

For example in the bat file executephp.bat, write this

c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php

save that bat file that contains that line.

Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.

For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.

Remember to invoke the script even when the user is not logged on.

Everything is set and it will execute without problem.

Eaint
  • 161
  • 1
  • 5
9

You can use PHP Command Line to execute it rather then trying to load it through the browser.

Under the actions tab, create a new action and:

  • Program/Script: Point to your PHP.exe file
  • Add Arguments: -f /path/to/php/file.php

Optionally you can make it start in the script's directory as well.

Brad Christie
  • 96,086
  • 15
  • 143
  • 191
5

I just wanted to leave what i had to do to get this working for server 2012. Which was what has previously been said but with added quotes and using the 'Add arguments' box. So in Task Scheduler->Actions->Edit Action.

Program/script: "C:\xampp\php\php.exe"
Add arguments: -f "<full path and filename>"
Start in: <Path to file>

(I had to use 'start in' as i referred to class's within the script)

Hope this helps.

Matt G
  • 51
  • 1
  • 1
5

create Schedule task Scheduler->Actions->Edit Action

if you have php file

Program/script: powershell 
Add arguments: curl http://localhost/demo/cron.php

if you have MVC/CMS URL

Program/script: powershell
Add arguments: curl http://localhost/demo/controller/method
3

Here's how I did it.

In the Run box: c:\location_of_my_php_installation\php.exe -f c:\location_of_my_php_file\php_file.php

In the Start in box: c:\location_of_my_php_installation\php.exe

Nightfirecat
  • 10,836
  • 6
  • 32
  • 50
Justin
  • 31
  • 1
3

you can directly call your local host url by using

explorer "http://localhost/yourFile.php"
Riyas PK
  • 2,467
  • 1
  • 19
  • 26
3

If the answer given by Pekka does not work (C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php), make sure that you have the correct PHP extensions enabled as well as the correct php.ini file being used for the PHP version you are using.

I ran into this issue recently and resolved it. I was using PHP v5.4 to run my script.php that was nested within the top level PHP folder (v5.3). When I ran the script.php, from within the v5.4 folder, it was using the v5.3 php.ini file with different extensions, which caused the script.php to fail.

To fix this, here is what I did within the Task Scheduler : Actions tab

Program Script:

"C:\Program Files (x86)\PHP\v5.4\php.exe"

Add Arguements:

-c "C:\Program Files (x86)\PHP\v5.4\php.ini" -f "C:\Xampp\htdocs\script.php"

Task Schedulerer - Actions Script

Using the -c option, you can specify which php.ini file should be used (stackflow answer).

I verified which php was in use by following this stackflow answer.

In command line, type php -m to check which extensions are enabled. Then, use php --ini to check which .ini file(s) is/are being read by PHP.

You may not be using the correct ini file for the PHP version you are using if you have multiple versions installed.

Pavel Smirnov
  • 4,293
  • 3
  • 14
  • 23
  • On error "The directory name is invalid. (0x8007010B)", remove the quotes of `Start in (optional)` field, doesn’t support them. [WINDOWS TASK SCHEDULER: THE DIRECTORY NAME IS INVALID. (0X8007010B)](http://matthewcevans.com/blog/2010/08/11/windows-task-scheduler-the-directory-name-is-invalid-0x8007010b/) – fernandoandrade Nov 30 '19 at 13:58
0

In the properties for your task in the Task Scheduler, change the account for which your script is running under to the SYSTEM account.

Beaware! You should ONLY do this for scripts you fully trust - Doing this elevates the privileges to of the script to beyond administrator.

chjortlund
  • 2,592
  • 26
  • 26
0

To create a scheduler in Windows, you need to:

  1. Create .bat file on your server or system;
  2. Type the following command in your.bat file:
    “F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
  3. Set the scheduler time and file in your task scheduler in Windows.
Pavel Smirnov
  • 4,293
  • 3
  • 14
  • 23
Sagar Jain
  • 11
  • 4
  • please include command (or commands?) into the code-formatting wrappers (backticks) and check your quotation symbols: they should probably be either `"` or omitted at all – YakovL Oct 01 '16 at 13:56
0

You dont need a .bat file just set the options in the task scheduler: Windows Server Task Scheduler

So here i've just pointed to the php.exe and then in the arguments the

-f fullpath2PHP file

The important bit is the run in part - this should be the folder your php sits in. so for example:

File location -f c:\inetpub\wwwroot\runthisplease\myFile.php Run in c:\inetpub\wwwroot\runthisplease\

You can test this by creating a windows shortcut (but you cannot schedule a shortcut link)

Bodkins
  • 21
  • 2