Editing
Execute php script in a bat file
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
How to execute php script in a batch (bat) file == BAT and Bash File Examples == === Example of BAT file for Windows === Condition 1: The DOS window was closed automatically when PHP script which implemented [http://php.net/manual/en/function.exit.php exit()] and return error code 1. <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> Condition 2: The DOS window was NOT closed automatically. It might annoying if there are too many DOS window. <pre> @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 </pre> === Example of BASH file for Linux === PHP script which implemented [http://php.net/manual/en/function.exit.php exit()] and return error code 1<ref>[https://stackoverflow.com/questions/6827837/accessing-errorlevel-from-bash-script cmd - accessing ERRORLEVEL from bash script - Stack Overflow]</ref><ref>[https://www.tldp.org/LDP/abs/html/comparison-ops.html Other Comparison Operators]</ref><ref>[https://unix.stackexchange.com/questions/293940/bash-how-can-i-make-press-any-key-to-continue Bash - How can I make "Press any key to continue" - Unix & Linux Stack Exchange]</ref>. <pre> #!/bin/bash path/to/php "path/to/php_script.php" -var1=value1 if [ $? -eq 1 ]; then read -n 1 -s -r -p "Press any key to continue" exit 1 fi </pre> == Use cases == === Case 1: Execute the PHP code directly === # Enter {{kbd | key=cmd}} to open Windows Command Line # Enter the following command in Windows Command Line or save as BAT file: <pre> c:\path\to\php.exe -r "echo 'Hello';" </pre> {{Tips}} If you do not know where PHP installed, you may open the DOS window ({{kbd | key=<nowiki>cmd</nowiki>}}) and key in {{kbd | key=<nowiki>where php</nowiki>}} to find the path. === Case 2: Execute the PHP script file without passing parameters === <pre> c:\path\to\php.exe -f "c:\path\to\php_script.php" or c:\path\to\php.exe "c:\path\to\php_script.php" </pre> === Case 3: Execute the PHP script file passing parameters === <pre> c:\path\to\php.exe -f "c:\path\to\php_script.php" -- -c=3 or c:\path\to\php.exe "c:\path\to\php_script.php" -a=2 </pre> Options "-f <file> Parse and execute <file>."<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref> == Run the PHP script automatically at a specified time == * {{Gd}} {{Win}} Using [https://www.splinterware.com/products/scheduler.html System Scheduler] software & specify the path to bat file. * {{Win}} Task Scheduler in Windows 10<ref>[https://windowsreport.com/schedule-tasks-windows-10/ How to schedule tasks in Windows 10]</ref> (Windows 系統管理工具 → 工作排程器<ref>[https://key.chtouch.com/ContentView.aspx?P=2487 如何利用 Windows 10 內建的工作排程器,設定電腦定時自動關機、休眠或自動重開機?]</ref>) * {{Linux}} [https://en.wikipedia.org/wiki/Cron Cron] == Troubleshootings == === Error: The system cannot find the path specified. === <pre> c:\path\to\php.exe -f "c:\path\to\php_script.php" </pre> Possible solutions: Check the php path if correct or not <pre> dir c:\path\to\php.exe </pre> === Error: Could not open input file === Error message<ref>[https://stackoverflow.com/questions/29435912/how-do-i-pass-parameters-to-a-php-script-in-a-bat-file windows - How do I pass parameters to a php script in a bat file - Stack Overflow]</ref>: Could not open input file The code caused the error message: <pre> c:\path\to\php.exe -f "c:\path\to\php_script.php" -var1=value1 </pre> Possible solutions: (1) add double dash symbol {{kbd | key=<nowiki>--</nowiki>}} after php script (2) remove {{kbd|key=<nowiki>-f</nowiki>}} option from command <pre> 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 </pre> (3) Enclose the whole path with double quote symbols <pre> 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" </pre> == Related articles == * [https://www.computerhope.com/echohlp.htm MS-DOS echo command help] * [https://ss64.com/nt/exit.html Exit - Terminate a script - Windows CMD - SS64.com] * {{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)] * [http://php.net/getopt PHP: getopt - Manual] * [[Boot up Tasks]] / [[Boot up Tasks#自動執行程式 | 自動執行程式]] * [[How to run a scheduled task in the background]] * [https://errerrors.blogspot.com/2019/01/how-do-I-run-a-php-script-using-windows-system-scheduler.html 如何設定 Windows 排程自動執行 PHP 程式] * [[A simple bat file template]] == References == <references /> [[Category:Programming]] [[Category:PHP]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Gd
(
edit
)
Template:Kbd
(
edit
)
Template:Linux
(
edit
)
Template:Tips
(
edit
)
Template:Win
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information