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...) |
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric) |
(One intermediate revision by one other user not shown) | |
(No difference)
|
Latest revision as of 08:16, 24 November 2010
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 | ||
TITLE CLI TEST BAT COLOR B0 @echo off cls echo. usr\local\php\php.exe -n test_1.php echo. :pause |
|
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 | ||
<?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 ?> |
|
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 | ||
<?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 ?> |
|
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 | ||
<?php $a=0; while($a !=10){ usleep(1000000); echo " ".$a."\n"; $a=$a+1; } exit(0); // Script ran OK ?> |
|
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
$cmd = 'start /b usr\local\php\php.exe -n test_2.php';
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
<?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 ?>
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) |