PHP CLI: Process Running: Difference between revisions

Jump to navigation Jump to search
m
Reverted edits by Upazixorys (Talk); changed back to last version by Ric
No edit summary
m (Reverted edits by Upazixorys (Talk); changed back to last version by Ric)
 
Line 1: Line 1:
----
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
----
=[http://uwujojedeh.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
----
=[http://uwujojedeh.co.cc CLICK HERE]=
----
</div>
{{Uc nav PHP CLI}}
{{Uc nav PHP CLI}}
'''''PHP CLI Check if a process (program) is running'''''
'''''PHP CLI Check if a process (program) is running'''''
Line 20: Line 12:
|-
|-
|'''''Run.bat'''''||'''''test_1.php''''';
|'''''Run.bat'''''||'''''test_1.php''''';
|-valign=&quot;top&quot;
|-valign="top"
|
|
&lt;pre&gt;
<pre>
TITLE CLI TEST BAT
TITLE CLI TEST BAT
COLOR B0
COLOR B0
Line 31: Line 23:
echo.
echo.
pause
pause
&lt;/pre&gt;
</pre>
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
   print_r(win32_ps_list_procs());
   print_r(win32_ps_list_procs());
   echo &quot;Hello World&quot;;
   echo "Hello World";
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
|}
|}
'''''Note'':''' Above assumes you have installed the extension '''php_win32ps.dll''' as described on the [[PHP CLI: PHP INI#Extension download | previous page]].
'''''Note'':''' Above assumes you have installed the extension '''php_win32ps.dll''' as described on the [[PHP CLI: PHP INI#Extension download | previous page]].
Line 50: Line 42:
=== Reduce output ===
=== Reduce output ===
Running the above example produces an overwhelming amount of data lets reduce this by displaying only '''pids''' and '''executable path-names''', edit test_1.php as follows:
Running the above example produces an overwhelming amount of data lets reduce this by displaying only '''pids''' and '''executable path-names''', edit test_1.php as follows:
&lt;pre&gt;
<pre>
&lt;?php
<?php
//print_r(win32_ps_list_procs());
//print_r(win32_ps_list_procs());


Line 59: Line 51:
$pid = $processArray['pid'];
$pid = $processArray['pid'];
$exe = $processArray['exe'];
$exe = $processArray['exe'];
echo &quot;$pid  $exe \n&quot;;
echo "$pid  $exe \n";
}
}
//echo &quot;Hello World&quot;;
//echo "Hello World";
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
This produces a list of all running processes similar to this small extract:
This produces a list of all running processes similar to this small extract:
&lt;pre&gt;
<pre>
3240  C:\Program Files\Mozilla Firefox\firefox.exe
3240  C:\Program Files\Mozilla Firefox\firefox.exe
3660  C:\WINDOWS\System32\svchost.exe
3660  C:\WINDOWS\System32\svchost.exe
Line 78: Line 70:
2916  H:\z_test_42\UniServer\udrive\usr\local\php\php.exe
2916  H:\z_test_42\UniServer\udrive\usr\local\php\php.exe
…… etc
…… etc
&lt;/pre&gt;
</pre>


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
Line 84: Line 76:
=== Check a process is running ===
=== Check a process is running ===
This script shows how to target a process by name (example targets '''cmd.exe''') and check if it is currently running, change '''test_1.php''' as follows:
This script shows how to target a process by name (example targets '''cmd.exe''') and check if it is currently running, change '''test_1.php''' as follows:
&lt;pre&gt;
<pre>
&lt;?php
<?php
$processName = 'cmd.exe';
$processName = 'cmd.exe';
@$processList = win32_ps_list_procs();
@$processList = win32_ps_list_procs();
Line 92: Line 84:
   $exe = $processArray['exe'];
   $exe = $processArray['exe'];
   if (basename($processArray['exe']) == $processName){
   if (basename($processArray['exe']) == $processName){
     echo &quot;\n Process $processName is running\n&quot;;
     echo "\n Process $processName is running\n";
     echo &quot;$pid  $exe \n&quot;;
     echo "$pid  $exe \n";
     break;
     break;
   }
   }
}
}
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
Run the batch file (double click on Run.bat) output will be similar to this:
Run the batch file (double click on Run.bat) output will be similar to this:
&lt;pre&gt;
<pre>
  Process cmd.exe is running
  Process cmd.exe is running
1896  C:\WINDOWS\system32\cmd.exe
1896  C:\WINDOWS\system32\cmd.exe
&lt;/pre&gt;
</pre>
The above can be converted into a function for general purpose however there is a small problem.
The above can be converted into a function for general purpose however there is a small problem.


Line 112: Line 104:
I purposefully masked an issue with the above script. Comment out the break line as shown.  
I purposefully masked an issue with the above script. Comment out the break line as shown.  
{|
{|
|-valign=&quot;top&quot;
|-valign="top"
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
$processName = 'cmd.exe';
$processName = 'cmd.exe';
@$processList = win32_ps_list_procs();
@$processList = win32_ps_list_procs();
Line 122: Line 114:
   $exe = $processArray['exe'];
   $exe = $processArray['exe'];
   if (basename($processArray['exe']) == $processName){
   if (basename($processArray['exe']) == $processName){
     echo &quot;\n Process $processName is running\n&quot;;
     echo "\n Process $processName is running\n";
     echo &quot;$pid  $exe \n&quot;;
     echo "$pid  $exe \n";
//    break;
//    break;
   }
   }
}
}
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
|
|
Run the batch file (double click on Run.bat) several times
Run the batch file (double click on Run.bat) several times


Result similar to this&gt;
Result similar to this>
&lt;pre&gt;
<pre>
  Process cmd.exe is running
  Process cmd.exe is running
3860  C:\WINDOWS\system32\cmd.exe
3860  C:\WINDOWS\system32\cmd.exe
Line 143: Line 135:
  Process cmd.exe is running
  Process cmd.exe is running
3788  C:\WINDOWS\system32\cmd.exe
3788  C:\WINDOWS\system32\cmd.exe
&lt;/pre&gt;
</pre>
|}
|}
There are three command processes running each with an identical name, if you were to kill the process by name all three processes are killed. To kill a single process use its PID this is always unique.
There are three command processes running each with an identical name, if you were to kill the process by name all three processes are killed. To kill a single process use its PID this is always unique.
Line 157: Line 149:
=== Kill process by name ===
=== Kill process by name ===
This script uses a function to kill a named process, edit test_1.php and run the script (Run.bat)
This script uses a function to kill a named process, edit test_1.php and run the script (Run.bat)
&lt;pre&gt;
<pre>
&lt;?php
<?php
kill_proc(&quot;cmd.exe&quot;);
kill_proc("cmd.exe");


function kill_proc($processName){
function kill_proc($processName){
Line 166: Line 158:
         $pid = $processArray['pid'];
         $pid = $processArray['pid'];
         if (basename($processArray['exe']) == $processName){           
         if (basename($processArray['exe']) == $processName){           
           `taskkill /f /PID $pid 2&gt;&amp;1`;                   
           `taskkill /f /PID $pid 2>&1`;                   
         }
         }
     }
     }
}
}
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
Well if you are running XP-Pro '''Run.bat''' will have been killed. However if you are running XP-Home the process is not killed with '''taskkill'''. To keep both XP-Pro and XP-Home happy change the script to:
Well if you are running XP-Pro '''Run.bat''' will have been killed. However if you are running XP-Home the process is not killed with '''taskkill'''. To keep both XP-Pro and XP-Home happy change the script to:
&lt;pre&gt;
<pre>
&lt;?php
<?php
kill_proc(&quot;cmd.exe&quot;);
kill_proc("cmd.exe");


function kill_proc($processName){
function kill_proc($processName){
Line 188: Line 180:
}
}
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
There is no end of these examples where an XP-Pro user (designer) never seeing or considering this type of issue, anyway I think the above vindicates my '''general note''' in the opening statement.
There is no end of these examples where an XP-Pro user (designer) never seeing or considering this type of issue, anyway I think the above vindicates my '''general note''' in the opening statement.


Line 220: Line 212:
|-
|-
|'''''Run.bat'''''
|'''''Run.bat'''''
|-valign=&quot;top&quot;
|-valign="top"
|
|
&lt;pre&gt;
<pre>
TITLE CLI TEST BAT
TITLE CLI TEST BAT
COLOR B0
COLOR B0
Line 231: Line 223:
echo.
echo.
pause
pause
&lt;/pre&gt;
</pre>
|}
|}


Line 241: Line 233:
|-
|-
|'''''test_1.php''''';
|'''''test_1.php''''';
|-valign=&quot;top&quot;
|-valign="top"
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
  $exe_name = &quot;notepad.exe&quot;;                      // process to check
  $exe_name = "notepad.exe";                      // process to check
  $command= &quot;unicon\program\pskill.exe $exe_name&quot;; // command line to be run   
  $command= "unicon\program\pskill.exe $exe_name"; // command line to be run   
  exec($command,$dummy,$return);                  // 0=running 1=not-running  
  exec($command,$dummy,$return);                  // 0=running 1=not-running  


  if($return == 0){                                // Check return value
  if($return == 0){                                // Check return value
   echo  &quot; $exe_name Is running&quot;;
   echo  " $exe_name Is running";
  }
  }
  else{
  else{
   echo  &quot; $exe_name Not running&quot;;
   echo  " $exe_name Not running";
  }
  }
exit(0); // Script ran OK
exit(0); // Script ran OK
?&gt;
?>
&lt;/pre&gt;
</pre>
|}
|}


Line 287: Line 279:
{|
{|
|-
|-
|'''''test_1.php'''''||&amp;nbsp;
|'''''test_1.php'''''||&nbsp;
|-valign=&quot;top&quot;
|-valign="top"
|
|
&lt;pre&gt;
<pre>
&lt;?php
<?php
  $exe_name = &quot;notepad.exe&quot;;        // process to kill
  $exe_name = "notepad.exe";        // process to kill


  // command line to be run note c as second parameter kills
  // command line to be run note c as second parameter kills
  // the above named process   
  // the above named process   


  $command= &quot;unicon\program\pskill.exe $exe_name c&quot;;  
  $command= "unicon\program\pskill.exe $exe_name c";  
  exec($command);                  // runs command   
  exec($command);                  // runs command   
  exit(0);                          // Script ran OK
  exit(0);                          // Script ran OK
?&gt;
?>
?&gt;
?>
&lt;/pre&gt;
</pre>
|
|
&lt;br /&gt;
<br />
'''''Test'':'''
'''''Test'':'''
* Run Notepad
* Run Notepad

Navigation menu