Execute php script in a bat file: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 21: | Line 21: | ||
Options "-f <file> Parse and execute <file>."<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref> | Options "-f <file> Parse and execute <file>."<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref> | ||
== Sample bat file == | |||
<pre> | |||
@ECHO OFF | |||
c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1 | |||
PAUSE | |||
</pre> | |||
instruction: | |||
* [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> | |||
== Application: Run the PHP script automatically at a specified time == | == Application: Run the PHP script automatically at a specified time == | ||
Revision as of 12:09, 1 March 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
@ECHO OFF c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1 PAUSE
instruction:
- MS-DOS echo command help
- PAUSE at the end of file to prevent auto closing of console window after the execution of batch file. [2][3]
Application: Run the PHP script automatically at a specified time
Win
Using System Scheduler software & specify the path to bat file.- Win
Task Scheduler in Windows 10[4] (Windows 系統管理工具 → 工作排程器[5]) - Linux
Cron
Troubleshootings
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
Solution: (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
References
- ↑ PHP: Options - Manual
- ↑ MS-DOS pause command help
- ↑ windows - How to prevent auto-closing of console after the execution of batch file. - Stack Overflow
- ↑ How to schedule tasks in Windows 10
- ↑ 如何利用 Windows 10 內建的工作排程器,設定電腦定時自動關機、休眠或自動重開機?
- ↑ windows - How do I pass parameters to a php script in a bat file - Stack Overflow