Execute php script in a bat file: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
Line 23: Line 23:


== Sample bat file ==
== Sample bat file ==
Condition1: The DOS window was NOT closed when PHP script which implemented [http://php.net/manual/en/function.exit.php exit()] and returned error.
<pre>
@ECHO OFF
c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1
if NOT ["%errorlevel%"]==["0"] (
    PAUSE
    exit /b %errorlevel%
)
</pre>
Condition2: The DOS window was NOT closed automatically. It might annoying if there are too many window.
<pre>
<pre>
@ECHO OFF
@ECHO OFF
Line 33: Line 44:
* [https://www.computerhope.com/echohlp.htm MS-DOS echo command help]
* [https://www.computerhope.com/echohlp.htm MS-DOS echo command help]
* {{kbd | key=<nowiki>PAUSE</nowiki>}} at the end of file to prevent auto closing of console window after the execution of batch file. <ref>[https://www.computerhope.com/pausehlp.htm MS-DOS pause command help]</ref><ref>[https://stackoverflow.com/questions/988403/how-to-prevent-auto-closing-of-console-after-the-execution-of-batch-file windows - How to prevent auto-closing of console after the execution of batch file. - Stack Overflow]</ref>
* {{kbd | key=<nowiki>PAUSE</nowiki>}} at the end of file to prevent auto closing of console window after the execution of batch file. <ref>[https://www.computerhope.com/pausehlp.htm MS-DOS pause command help]</ref><ref>[https://stackoverflow.com/questions/988403/how-to-prevent-auto-closing-of-console-after-the-execution-of-batch-file windows - How to prevent auto-closing of console after the execution of batch file. - Stack Overflow]</ref>
* [https://coderwall.com/p/jexjlw/pause-on-error-in-batch-file Pause on error in Batch File (Example)]


== Application: Run the PHP script automatically at a specified time ==
== Application: Run the PHP script automatically at a specified time ==

Revision as of 17:21, 22 August 2018

How to execute php script in a bat file

Case 1: Execute the PHP script without passing parameters

c:\path\to\php.exe -f "c:\path\to\php_script.php"

or

c:\path\to\php.exe "c:\path\to\php_script.php"

Case 2: Execute the PHP script passing parameters

c:\path\to\php.exe -f "c:\path\to\php_script.php" -- -var1=value1

or

c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1


Options "-f <file> Parse and execute <file>."[1]

Sample bat file

Condition1: The DOS window was NOT closed when PHP script which implemented exit() and returned error.

@ECHO OFF
c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1
if NOT ["%errorlevel%"]==["0"] (
    PAUSE
    exit /b %errorlevel%
)

Condition2: The DOS window was NOT closed automatically. It might annoying if there are too many window.

@ECHO OFF
c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1
c:\path\to\php.exe "c:\path\to\php_script2.php" -var2=value2
PAUSE

instruction:

Application: Run the PHP script automatically at a specified time

  • Good.gif Win Os windows.png Using System Scheduler software & specify the path to bat file.
  • Win Os windows.png Task Scheduler in Windows 10[4] (Windows 系統管理工具 → 工作排程器[5])
  • Linux Os linux.png Cron

Troubleshootings

Error: The system cannot find the path specified.

c:\path\to\php.exe -f "c:\path\to\php_script.php"

Possible solutions: Check the php path if correct or not

dir c:\path\to\php.exe

Error: Could not open input file

Error message[6]: Could not open input file

The code caused the error message:

c:\path\to\php.exe -f "c:\path\to\php_script.php" -var1=value1

Possible solutions: (1) add -- after php script (2) remove -f option from command

c:\path\to\php.exe -f "c:\path\to\php_script.php" -- -var1=value1

or

c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1

(3) Enclose the whole path with double quote symbols

Wrong: c:\path\to\php.exe c:\path\contain white space\php_script.php

Correct: c:\path\to\php.exe "c:\path\contain white space\php_script.php"

References

Related articles