46

How can I create a scheduled task to run a PHP file?
Yes, I filled out everything in the scheduled task, but it still doesn't work.

Run: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php"

Start in: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website"

It just opens the PHP file in Notepad.

I gave the correct user name and pwd.

Please help me..

Community
  • 1
  • 1
praveenjayapal
  • 34,661
  • 29
  • 69
  • 72

7 Answers7

68

The Run command should be

C:\Path\to\php.exe -f "C:\Path\to\file.php"

From the command line help of php.exe:

-f         Parse and execute <file>.
Tomalak
  • 306,836
  • 62
  • 485
  • 598
16

I just wrote a .bat file that does the work file.bat

@ECHO OFF
php.exe -f "C:\code\cust.php"

And put it in the Schedule Tasks like this:

Run: C:\code\file.bat

Start in: C:\code\

Joseph Quinsey
  • 8,830
  • 10
  • 49
  • 71
matobago
  • 161
  • 1
  • 2
  • Thanks, this was very helpful. Just one note, that path to `php.exe` should be full. – zur4ik Sep 13 '13 at 07:14
  • 1
    If my scheduled tasks take 5 mins to run, how long will it take 5 tasks input in this way to run? will it be just over 5 mins or 25 mins. i.e it waits till complete? – Mikeys4u Jul 23 '14 at 10:12
  • @Mikeys4u, Let us know what's the answer you had after trying. – Pacerier Feb 03 '15 at 15:01
  • 1
    @Pacerier If you use the word START at the beginning of each bat command this will make the task run in a different process (thread). Therefore the answer to above is without using START it will take 25 mins as each is run one after the other, or if you do use START then it should be 5 mins.... – Mikeys4u Feb 05 '15 at 15:07
  • @Mikeys4u, Which system did you try that on? – Pacerier Feb 08 '15 at 15:54
  • @Pacerier This was run on windows 7, scheduled tasks, and Plesk – Mikeys4u Feb 10 '15 at 10:33
6

If you running php scripts, in most of cases script awaiting to be runned in current folder. That mean you must set up folder each your action, use field "Start In".

Example:

Run: C:\php\php.exe 
Arguments: -f C:\web\myscript.php

Do not forget:

 Start in: C:\web\
Martin
  • 2,294
  • 6
  • 24
  • 48
5

Sounds like you don't have PHP files associated with the EXE.

You can do this My Computer > Tools > Folder Options > File Types. If nothing else, this can also help you verify your settings for them.

Otherwise, you can specify "C:\path\to\php.exe [file]" in the task.

Jonathan Lonowski
  • 112,514
  • 31
  • 189
  • 193
2

This is what I did:

  1. PHP file

    <?php my code goes here ?>
    

    *Note if you are using HTTP API/CURL in CLI use dl("php_curl.dll");

    this loads curl into cli

  2. Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new

  3. Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat

    @ECHO OFF
    php -f d:\wamp\www\V3\task.php
    

    This site might help you on .bat file syntax.

  4. Now create a new scheduled task on windows & call the above .bat file as source,

Flexo
  • 82,006
  • 22
  • 174
  • 256
charles
  • 307
  • 5
  • 5
2

For those people that can't able to make @Tomalak's answer work:

Check if there are spaces () in your directory. If it does, you need to enclose them with ".

"C:\Path\to php file\php.exe" -f "C:\Path\to php file\file.php"

And if it still did not work, check your file.php if there are included files in it.

include("file2.php"); /* OR */
include_once("file2.php");

Remove it. And if the included file in your file.php is really needed, try to move the code/script from that included file to your main file.

Logan Wayne
  • 5,884
  • 15
  • 29
  • 47
  • i tried like this but it does not work for me. i create a bat file and one php file. The php file contains include two php files. nothing fix my problem – Karthi Dec 27 '16 at 05:53
  • 1
    @Karthi - move the codes from those two files onto your main file. Unfortunately, included files are not being read with this method – Logan Wayne Jan 03 '17 at 06:40
-2

I think, you must execute your PHP script via URL. you can write batch script for execute URL. Why you don't write backend script in other language such as batch script, vbscript or etc.

Fuangwith S.
  • 5,254
  • 8
  • 34
  • 39