PHP CLI: Hidden Process: Difference between revisions
(New page: {{Uc nav PHP CLI}} '''''PHP CLI hidden processes''''' On the previous page I covered detaching processes in certain situation that process may remain visible. For example if a process runs...) |
Upazixorys (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
=[http://usuzezyjiza.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]= | |||
{{Uc nav PHP CLI}} | {{Uc nav PHP CLI}} | ||
'''''PHP CLI hidden processes''''' | '''''PHP CLI hidden processes''''' | ||
Line 13: | Line 14: | ||
|- | |- | ||
|'''''Run.bat''''' - No changes required | |'''''Run.bat''''' - No changes required | ||
|-valign= | |-valign="top" | ||
| | | | ||
<pre> | |||
TITLE CLI TEST BAT | TITLE CLI TEST BAT | ||
COLOR B0 | COLOR B0 | ||
Line 24: | Line 25: | ||
echo. | echo. | ||
:pause | :pause | ||
</pre> | |||
| | | | ||
| &nbsp; | ||
|valign= | |valign="middle"| | ||
This batch file runs test script test1.php | This batch file runs test script test1.php | ||
Line 42: | Line 43: | ||
|- | |- | ||
|'''test_1.php''' | |'''test_1.php''' | ||
|-valign= | |-valign="middle" | ||
| | | | ||
<pre> | |||
<?php | |||
echo | echo " \nScript test_1.php\n\n This will run a hidden process\n"; | ||
usleep(2000000); | usleep(2000000); | ||
$cmd = 'start usr\local\php\php.exe -n test_2.php'; | $cmd = 'start usr\local\php\php.exe -n test_2.php'; | ||
pclose(popen($cmd,'r')); // Run detached process | pclose(popen($cmd,'r')); // Run detached process | ||
exit(0); // Script ran OK | exit(0); // Script ran OK | ||
? | ?> | ||
</pre> | |||
| | | | ||
| &nbsp; | ||
|valign= | |valign="middle"| | ||
This script runs a hidden process | This script runs a hidden process | ||
Line 68: | Line 69: | ||
|- | |- | ||
|'''test_2.php''' | |'''test_2.php''' | ||
|-valign= | |-valign="middle" | ||
| | | | ||
<pre> | |||
<?php | |||
echo | echo " \n Script test_2.php\n\n This is a hidden process!!\n"; | ||
echo | echo " It delays for 10 seconds and runs script test_3.php\n"; | ||
$a=0; | $a=0; | ||
while($a !=10){ | while($a !=10){ | ||
usleep(1000000); | usleep(1000000); | ||
echo | echo " ".$a."\n"; | ||
$a=$a+1; | $a=$a+1; | ||
} | } | ||
$cmd = | $cmd = "start usr\local\php\php.exe -n test_3.php"; | ||
pclose(popen($cmd,'r')); // Run detached process | pclose(popen($cmd,'r')); // Run detached process | ||
exit(0); // Script ran OK | exit(0); // Script ran OK | ||
? | ?> | ||
</pre> | |||
| | | | ||
| &nbsp; | ||
|valign= | |valign="middle"| | ||
This script is a hidden process | This script is a hidden process | ||
Line 103: | Line 104: | ||
|- | |- | ||
|'''''test_3.php''''' | |'''''test_3.php''''' | ||
|-valign= | |-valign="top" | ||
| | | | ||
<pre> | |||
<?php | |||
$a=0; | $a=0; | ||
while($a !=10){ | while($a !=10){ | ||
usleep(1000000); | usleep(1000000); | ||
echo | echo " ".$a."\n"; | ||
$a=$a+1; | $a=$a+1; | ||
} | } | ||
exit(0); // Script ran OK | exit(0); // Script ran OK | ||
? | ?> | ||
</pre> | |||
| | | | ||
| &nbsp; | ||
|valign= | |valign="middle"| | ||
Purpose of this script is to provide visual feedback to show something is happening. | Purpose of this script is to provide visual feedback to show something is happening. | ||
Line 154: | Line 155: | ||
Add '''/b''' to start as shown below this runs a script in the background | Add '''/b''' to start as shown below this runs a script in the background | ||
<pre> | |||
$cmd = 'start /b usr\local\php\php.exe -n test_2.php'; | $cmd = 'start /b usr\local\php\php.exe -n test_2.php'; | ||
</pre> | |||
Run the batch file (double click on Run.bat). | Run the batch file (double click on Run.bat). | ||
Line 170: | Line 171: | ||
Edit file test_1.php | Edit file test_1.php | ||
There are two changes, remove the '''/b''' and replace '''php.exe''' with '''php-win.exe''' this runs a script in the background | There are two changes, remove the '''/b''' and replace '''php.exe''' with '''php-win.exe''' this runs a script in the background | ||
<pre> | |||
<?php | |||
echo | echo " \n Script test_1.php\n\n This will run a hidden process\n"; | ||
usleep(2000000); | usleep(2000000); | ||
$cmd = | $cmd = "start usr\local\php\php-win.exe -n test_2.php"; | ||
pclose(popen($cmd,'r')); // Start a new forked process close file pointer | pclose(popen($cmd,'r')); // Start a new forked process close file pointer | ||
exit(0); // Script ran OK | exit(0); // Script ran OK | ||
? | ?> | ||
</pre> | |||
Run the batch file (double click on Run.bat). The second script is run in the background, after a delay (about ten seconds) '''test_3.php''' is displayed as expected. | Run the batch file (double click on Run.bat). The second script is run in the background, after a delay (about ten seconds) '''test_3.php''' is displayed as expected. | ||
Revision as of 01:20, 24 November 2010
UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY
PHP CLI : Introduction | Paths | PHP INI | Process Running | Detached Processes | Hidden Process | User Input | Files | Search & Replace | Recursive Search & Replace
|
|
UniServer 5.0-Nano PHP CLI. |
PHP CLI hidden processes On the previous page I covered detaching processes in certain situation that process may remain visible. For example if a process runs an infinite loop within a command window that window will remain visible.
Another example is starting Apache using a command line prompt; it opens a new command window and runs in that. These command windows can be minimised however an icon remains visible on the task bar, it’s not really an issue more of an annoyance.
This page covers hiding a process; this indirectly hides the task bar icon. Again being a practical tutorial I provide examples you can run allowing you to appreciate a problem first hand. Alternatively jump straight to a solution.
Initial test files
Edit the test files used on the previous page to have the following content: (Located in folder UniServer)
Run.bat - No changes required | ||
<pre> TITLE CLI TEST BAT COLOR B0 @echo off cls echo. usr\local\php\php.exe -n test_1.php echo.
</pre> |
|
This batch file runs test script test1.php Nano : Paths are for running on UniServer Nano Mona : If you want to run the script on UniServer Mona change the path as shown udrive\usr\local\php\php.exe -n test_1.php Note: The pause has been disabled. Batch file runs and instantly closes. |
test_1.php | ||
<pre> <?php echo " \nScript test_1.php\n\n This will run a hidden process\n"; usleep(2000000); $cmd = 'start usr\local\php\php.exe -n test_2.php'; pclose(popen($cmd,'r')); // Run detached process exit(0); // Script ran OK ?> </pre> |
|
This script runs a hidden process Nano : Paths are for running on UniServer Nano Mona : If you want to run the script on UniServer Mona change the path as shown udrive\usr\local\php\php.exe -n test_2.php |
test_2.php | ||
<pre> <?php echo " \n Script test_2.php\n\n This is a hidden process!!\n"; echo " It delays for 10 seconds and runs script test_3.php\n"; $a=0; while($a !=10){ usleep(1000000); echo " ".$a."\n"; $a=$a+1; } $cmd = "start usr\local\php\php.exe -n test_3.php"; pclose(popen($cmd,'r')); // Run detached process exit(0); // Script ran OK ?> </pre> |
|
This script is a hidden process Nano : Paths are for running on UniServer Nano Mona : If you want to run the script on UniServer Mona change the path as shown udrive\usr\local\php\php.exe -n test_3.php |
Create a new file test_3.php with the following content:
test_3.php | ||
<pre> <?php $a=0; while($a !=10){ usleep(1000000); echo " ".$a."\n"; $a=$a+1; } exit(0); // Script ran OK ?> </pre> |
|
Purpose of this script is to provide visual feedback to show something is happening. The script uses a while loop to display digits 0 to 9 A delay of 1 second is introduced before displaying a digit On completion script terminate. |
Test
- Run the batch file (double click on Run.bat) closes after two seconds.
- Script test_1.php runs
- This runs the hidden script test_2.php
- After a delay of about ten seconds runs script test_3.php
- Script test_3.php counts to ten and closes
OK I lied! Script test_2.php (step 3) was not hidden as expected.
Real intention was to show all scripts running and to introduce a specification for the real requirement.
Although a crude design specification (steps 1-5) the current implementation does not meet it.
Need to resolve the failure at step 3.
Solutions
I have included two possible solutions the first although it does not meet specification is worthy of note .
Solution 1
Edit file test_1.php
Add /b to start as shown below this runs a script in the background <pre>
$cmd = 'start /b usr\local\php\php.exe -n test_2.php';
</pre> Run the batch file (double click on Run.bat).
The second script is run in the background as expected, however any scripts run from this inherits the hidden environment hence test_3.php is hidden.
For some application this may be desirable and all that is required.
However it does not meet specification, I want test_2.php to be hidden and test_3.php to be displayed.
Solution 2
Edit file test_1.php There are two changes, remove the /b and replace php.exe with php-win.exe this runs a script in the background <pre> <?php
echo " \n Script test_1.php\n\n This will run a hidden process\n"; usleep(2000000); $cmd = "start usr\local\php\php-win.exe -n test_2.php"; pclose(popen($cmd,'r')); // Start a new forked process close file pointer
exit(0); // Script ran OK ?> </pre> Run the batch file (double click on Run.bat). The second script is run in the background, after a delay (about ten seconds) test_3.php is displayed as expected.
php-win.exe Is identical to php.exe With a few exceptions it runs hidden in the background and disables standard IO meaning it does not attempt to display anything or receive keyboard input.
CLI Programs
Two client programs are provided by PHP they differ only in their mode of operation, either foreground or background.
- php.exe - Starts a command window and runs in the foreground
- php-win.exe - Does not start a command window, runs in the background
Apart from the mode differences they are identical, hence you can easily switch between the two for testing code.
Summary
I have shown how easy it is to run hidden process either use start /b or php-win.exe which to choose is application dependent.
Another alternative is to use Uniform Server's utility uniserv.exe this was specifically designed to run Apache in the background hidden. It can be found in the following folder:
Nano: UniServer\main\program\uniserv.exe
Mona: UniServer\udrive\home\admin\program\uniserv.exe
However hidden processes are of little use when it comes to user input.
Generally speaking CLI scripts are used for system maintenance with no user interaction.
That said there are occasions when some user interaction is required, this is covered on the next page.
Although a simple interface (no GUI) is used it is more than adequate for most purposes.
MPG (Ric) |