PHP CLI: Hidden Process: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
No edit summary
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric)
 
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 14: Line 13:
|-
|-
|'''''Run.bat''''' - No changes required
|'''''Run.bat''''' - No changes required
|-valign="top"
|-valign="top"
|
|
<pre>
<pre>
TITLE CLI TEST BAT
TITLE CLI TEST BAT
COLOR B0
COLOR B0
Line 25: Line 24:
echo.
echo.
:pause
:pause
&lt;/pre&gt;
</pre>
|
|
&amp;nbsp;
&nbsp;
|valign=&quot;middle&quot;|
|valign="middle"|
This batch file runs test script test1.php
This batch file runs test script test1.php


Line 43: Line 42:
|-
|-
|'''test_1.php'''
|'''test_1.php'''
|-valign=&quot;middle&quot;
|-valign="middle"
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
   echo &quot; \nScript test_1.php\n\n This will run a hidden process\n&quot;;
   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
?&gt;
?>
&lt;/pre&gt;
</pre>
|
|
&amp;nbsp;
&nbsp;
|valign=&quot;middle&quot;|
|valign="middle"|
This script runs a hidden process
This script runs a hidden process


Line 69: Line 68:
|-
|-
|'''test_2.php'''
|'''test_2.php'''
|-valign=&quot;middle&quot;
|-valign="middle"
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
   echo &quot; \n Script test_2.php\n\n This is a hidden process!!\n&quot;;
   echo " \n Script test_2.php\n\n This is a hidden process!!\n";
   echo &quot; It delays for 10 seconds and runs script test_3.php\n&quot;;
   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 &quot; &quot;.$a.&quot;\n&quot;;
  echo " ".$a."\n";
  $a=$a+1;
  $a=$a+1;
}
}


$cmd = &quot;start usr\local\php\php.exe -n test_3.php&quot;;
$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
?&gt;
?>
&lt;/pre&gt;
</pre>
|
|
&amp;nbsp;
&nbsp;
|valign=&quot;middle&quot;|
|valign="middle"|
This script is a hidden process
This script is a hidden process


Line 104: Line 103:
|-
|-
|'''''test_3.php'''''
|'''''test_3.php'''''
|-valign=&quot;top&quot;
|-valign="top"
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
$a=0;
$a=0;
while($a !=10){
while($a !=10){
   usleep(1000000);  
   usleep(1000000);  
  echo &quot; &quot;.$a.&quot;\n&quot;;
  echo " ".$a."\n";
  $a=$a+1;
  $a=$a+1;
}
}
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
|
|
&amp;nbsp;
&nbsp;
|valign=&quot;middle&quot;|
|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 155: Line 154:


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
&lt;pre&gt;
<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';
&lt;/pre&gt;
</pre>
Run the batch file (double click on Run.bat).
Run the batch file (double click on Run.bat).


Line 171: Line 170:
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  
&lt;pre&gt;
<pre>
&lt;?php
<?php
   echo &quot; \n Script test_1.php\n\n This will run a hidden process\n&quot;;
   echo " \n Script test_1.php\n\n This will run a hidden process\n";
   usleep(2000000);
   usleep(2000000);
   $cmd = &quot;start usr\local\php\php-win.exe -n test_2.php&quot;;
   $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
?&gt;
?>
&lt;/pre&gt;  
</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.



Latest revision as of 08:16, 24 November 2010

 

MPG UniCenter

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.

Top

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.

Top

Test

  1. Run the batch file (double click on Run.bat) closes after two seconds.
  2. Script test_1.php runs
  3. This runs the hidden script test_2.php
  4. After a delay of about ten seconds runs script test_3.php
  5. 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.

Top

Solutions

I have included two possible solutions the first although it does not meet specification is worthy of note .

Top

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.

Top

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.

Top

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.

Top

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.

Top


MPG (Ric)