https://wiki.uniformserver.com/index.php?title=PHP_CLI:_Hidden_Process&feed=atom&action=historyPHP CLI: Hidden Process - Revision history2024-03-19T07:32:42ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=PHP_CLI:_Hidden_Process&diff=4756&oldid=prevOlajideolaolorun: Reverted edits by Upazixorys (Talk); changed back to last version by Ric2010-11-24T08:16:21Z<p>Reverted edits by <a href="/Special:Contributions/Upazixorys" title="Special:Contributions/Upazixorys">Upazixorys</a> (<a href="/index.php?title=User_talk:Upazixorys&action=edit&redlink=1" class="new" title="User talk:Upazixorys (page does not exist)">Talk</a>); changed back to last version by <a href="/User:Ric" title="User:Ric">Ric</a></p>
<a href="https://wiki.uniformserver.com/index.php?title=PHP_CLI:_Hidden_Process&diff=4756&oldid=4687">Show changes</a>Olajideolaolorunhttps://wiki.uniformserver.com/index.php?title=PHP_CLI:_Hidden_Process&diff=4687&oldid=prevUpazixorys at 01:20, 24 November 20102010-11-24T01:20:07Z<p></p>
<a href="https://wiki.uniformserver.com/index.php?title=PHP_CLI:_Hidden_Process&diff=4687&oldid=3821">Show changes</a>Upazixoryshttps://wiki.uniformserver.com/index.php?title=PHP_CLI:_Hidden_Process&diff=3821&oldid=prevRic: 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...2009-08-15T09:53:54Z<p>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...</p>
<p><b>New page</b></p><div>{{Uc nav PHP CLI}}<br />
'''''PHP CLI hidden processes'''''<br />
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.<br />
<br />
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.<br />
<br />
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 2|'''solution''']].<br />
<br />
'''''[[#top | Top]]'''''<br />
== Initial test files ==<br />
Edit the test files used on the previous page to have the following content: (Located in folder UniServer)<br />
{|<br />
|-<br />
|'''''Run.bat''''' - No changes required<br />
|-valign="top"<br />
|<br />
<pre><br />
TITLE CLI TEST BAT<br />
COLOR B0<br />
@echo off<br />
cls<br />
echo.<br />
usr\local\php\php.exe -n test_1.php<br />
echo.<br />
:pause<br />
</pre><br />
|<br />
&nbsp;<br />
|valign="middle"|<br />
This batch file runs test script test1.php<br />
<br />
'''''Nano :''''' Paths are for running on UniServer Nano<br />
<br />
'''''Mona :''''' If you want to run the script on UniServer Mona change the path as shown<br />
<br />
'''udrive\'''usr\local\php\php.exe -n test_1.php<br />
<br />
'''''Note'':''' The pause has been disabled. Batch file runs and instantly closes.<br />
|} <br />
<br />
{|<br />
|-<br />
|'''test_1.php'''<br />
|-valign="middle"<br />
|<br />
<pre><br />
<?php<br />
echo " \nScript test_1.php\n\n This will run a hidden process\n";<br />
usleep(2000000);<br />
$cmd = 'start usr\local\php\php.exe -n test_2.php';<br />
pclose(popen($cmd,'r')); // Run detached process <br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
|<br />
&nbsp;<br />
|valign="middle"|<br />
This script runs a hidden process<br />
<br />
'''''Nano :''''' Paths are for running on UniServer Nano<br />
<br />
'''''Mona :''''' If you want to run the script on UniServer Mona change the path as shown<br />
<br />
'''udrive\'''usr\local\php\php.exe -n test_2.php<br />
|}<br />
<br />
{|<br />
|-<br />
|'''test_2.php'''<br />
|-valign="middle"<br />
|<br />
<pre><br />
<?php<br />
echo " \n Script test_2.php\n\n This is a hidden process!!\n";<br />
echo " It delays for 10 seconds and runs script test_3.php\n";<br />
<br />
$a=0;<br />
while($a !=10){<br />
usleep(1000000); <br />
echo " ".$a."\n";<br />
$a=$a+1;<br />
}<br />
<br />
$cmd = "start usr\local\php\php.exe -n test_3.php";<br />
pclose(popen($cmd,'r')); // Run detached process <br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
|<br />
&nbsp;<br />
|valign="middle"|<br />
This script is a hidden process<br />
<br />
'''''Nano :''''' Paths are for running on UniServer Nano<br />
<br />
'''''Mona :''''' If you want to run the script on UniServer Mona change the path as shown<br />
<br />
'''udrive\'''usr\local\php\php.exe -n test_3.php<br />
|}<br />
<br />
Create a new file '''test_3.php''' with the following content:<br />
{|<br />
|-<br />
|'''''test_3.php'''''<br />
|-valign="top"<br />
|<br />
<pre><br />
<?php<br />
$a=0;<br />
while($a !=10){<br />
usleep(1000000); <br />
echo " ".$a."\n";<br />
$a=$a+1;<br />
}<br />
exit(0); // Script ran OK<br />
?><br />
</pre> <br />
|<br />
&nbsp;<br />
|valign="middle"|<br />
Purpose of this script is to provide visual feedback to show something is happening.<br />
<br />
The script uses a while loop to display digits 0 to 9<br />
<br />
A delay of 1 second is introduced before displaying a digit <br />
<br />
On completion script terminate. <br />
|} <br />
<br />
'''''[[#top | Top]]'''''<br />
=== Test ===<br />
# Run the batch file (double click on Run.bat) closes after two seconds.<br />
# Script '''test_1.php''' runs<br />
# This runs the hidden script '''test_2.php''' <br />
# After a delay of about ten seconds runs script '''test_3.php'''<br />
# Script '''test_3.php''' counts to ten and closes<br />
<br />
OK I lied! Script '''test_2.php''' (step 3) was not hidden as expected.<br />
<br />
Real intention was to show all scripts running and to introduce a specification for the real requirement.<br />
<br />
Although a crude design specification (steps 1-5) the current implementation does not meet it.<br />
<br />
Need to resolve the failure at step 3. <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Solutions ==<br />
I have included two possible solutions the first although it does not meet specification is worthy of note .<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Solution 1 ===<br />
Edit file '''test_1.php''' <br />
<br />
Add '''/b''' to start as shown below this runs a script in the background<br />
<pre><br />
$cmd = 'start /b usr\local\php\php.exe -n test_2.php';<br />
</pre><br />
Run the batch file (double click on Run.bat).<br />
<br />
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.<br />
<br />
For some application this may be desirable and all that is required.<br />
<br />
However it does not meet specification, I want test_2.php to be hidden and test_3.php to be displayed.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Solution 2 ===<br />
Edit file test_1.php<br />
There are two changes, remove the '''/b''' and replace '''php.exe''' with '''php-win.exe''' this runs a script in the background <br />
<pre><br />
<?php<br />
echo " \n Script test_1.php\n\n This will run a hidden process\n";<br />
usleep(2000000);<br />
$cmd = "start usr\local\php\php-win.exe -n test_2.php";<br />
pclose(popen($cmd,'r')); // Start a new forked process close file pointer<br />
exit(0); // Script ran OK<br />
?><br />
</pre> <br />
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.<br />
<br />
'''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. <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== CLI Programs ==<br />
Two client programs are provided by PHP they differ only in their mode of operation, either foreground or background.<br />
* '''php.exe''' - Starts a command window and runs in the foreground<br />
* '''php-win.exe''' - Does not start a command window, runs in the background<br />
<br />
Apart from the mode differences they are identical, hence you can easily switch between the two for testing code.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
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.<br />
<br />
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:<br />
<br />
'''''Nano'':''' UniServer\main\program\'''uniserv.exe'''<br />
<br />
'''''Mona'':''' UniServer\udrive\home\admin\program\'''uniserv.exe'''<br />
<br />
<br />
However hidden processes are of little use when it comes to user input.<br />
<br />
Generally speaking CLI scripts are used for system maintenance with no user interaction.<br />
<br />
That said there are occasions when some user interaction is required, this is covered on the [[PHP CLI: User Input | next page]].<br />
<br />
Although a simple interface (no GUI) is used it is more than adequate for most purposes.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
{| <br />
| [[Image:uc_small_logo.gif]] || '''MPG''' (Ric)<br />
|}<br />
<br />
[[Category: Tutorials]]<br />
[[Category: How To]]<br />
[[Category: Uniform Server 5.0-Nano]]</div>Ric