Execute php script in a bat file: Difference between revisions
Jump to navigation
Jump to search
m
no edit summary
No edit summary |
mNo edit summary |
||
| Line 1: | Line 1: | ||
How to execute php script in a batch (bat) file | How to execute php script in a batch (bat) file | ||
== Case 1: Execute the PHP code directly == | |||
== 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 {{kbd | key=cmd}} to open Windows Command Line | ||
| Line 12: | Line 45: | ||
: [[Image:Owl icon.jpg]] 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. | : [[Image:Owl icon.jpg]] 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 == | === Case 2: Execute the PHP script file without passing parameters === | ||
<pre> | <pre> | ||
c:\path\to\php.exe -f "c:\path\to\php_script.php" | c:\path\to\php.exe -f "c:\path\to\php_script.php" | ||
| Line 21: | Line 54: | ||
</pre> | </pre> | ||
== Case 3: Execute the PHP script file passing parameters == | === Case 3: Execute the PHP script file passing parameters === | ||
<pre> | <pre> | ||
c:\path\to\php.exe -f "c:\path\to\php_script.php" -- -c=3 | c:\path\to\php.exe -f "c:\path\to\php_script.php" -- -c=3 | ||
| Line 33: | Line 66: | ||
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> | ||
== Run the PHP script automatically at a specified time == | == Run the PHP script automatically at a specified time == | ||