Execute php script in a bat file
Jump to navigation
Jump to search
How to execute php script in a batch (bat) file
BAT and Bash File Examples[edit]
Example of BAT file for Windows[edit]
Condition 1: The DOS window was closed automatically when PHP script which implemented exit() and return error code 1.
@ECHO OFF c:\path\to\php.exe "c:\path\to\php_script.php" -var1=value1 if NOT ["%errorlevel%"]==["0"] ( PAUSE exit /b %errorlevel% )
Condition 2: The DOS window was NOT closed automatically. It might annoying if there are too many DOS 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
Example of BASH file for Linux[edit]
PHP script which implemented exit() and return error code 1[1][2][3].
#!/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
Use cases[edit]
Case 1: Execute the PHP code directly[edit]
- Enter cmd to open Windows Command Line
- Enter the following command in Windows Command Line or save as BAT file:
c:\path\to\php.exe -r "echo 'Hello';"
If you do not know where PHP installed, you may open the DOS window (cmd) and key in where php to find the path.
Case 2: Execute the PHP script file without passing parameters[edit]
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 3: Execute the PHP script file passing parameters[edit]
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
Options "-f <file> Parse and execute <file>."[4]
Run the PHP script automatically at a specified time[edit]
Win
Using System Scheduler software & specify the path to bat file.
- Win
Task Scheduler in Windows 10[5] (Windows 系統管理工具 → 工作排程器[6])
- Linux
Cron
Troubleshootings[edit]
Error: The system cannot find the path specified.[edit]
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[edit]
Error message[7]: 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 double dash symbol -- 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"
Related articles[edit]
- MS-DOS echo command help
- Exit - Terminate a script - Windows CMD - SS64.com
- PAUSE at the end of file to prevent auto closing of console window after the execution of batch file. [8][9]
- Pause on error in Batch File (Example)
- PHP: getopt - Manual
- Boot up Tasks / 自動執行程式
- How to run a scheduled task in the background
- 如何設定 Windows 排程自動執行 PHP 程式
- A simple bat file template
References[edit]
- ↑ cmd - accessing ERRORLEVEL from bash script - Stack Overflow
- ↑ Other Comparison Operators
- ↑ Bash - How can I make "Press any key to continue" - Unix & Linux Stack Exchange
- ↑ PHP: Options - Manual
- ↑ 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
- ↑ MS-DOS pause command help
- ↑ windows - How to prevent auto-closing of console after the execution of batch file. - Stack Overflow