https://wiki.uniformserver.com/index.php?title=PHP_CLI:_Process_Running&feed=atom&action=historyPHP CLI: Process Running - Revision history2024-03-28T23:58:18ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=PHP_CLI:_Process_Running&diff=4917&oldid=prevOlajideolaolorun: Reverted edits by Upazixorys (Talk); changed back to last version by Ric2010-11-24T17:36: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:_Process_Running&diff=4917&oldid=4901">Show changes</a>Olajideolaolorunhttps://wiki.uniformserver.com/index.php?title=PHP_CLI:_Process_Running&diff=4901&oldid=prevUpazixorys at 10:27, 24 November 20102010-11-24T10:27:31Z<p></p>
<a href="https://wiki.uniformserver.com/index.php?title=PHP_CLI:_Process_Running&diff=4901&oldid=3819">Show changes</a>Upazixoryshttps://wiki.uniformserver.com/index.php?title=PHP_CLI:_Process_Running&diff=3819&oldid=prevRic: New page: {{Uc nav PHP CLI}} '''''PHP CLI Check if a process (program) is running''''' One the previous page I covered installing extension '''php_win32ps.dll''' which allows you to list statics fo...2009-08-15T09:52:07Z<p>New page: {{Uc nav PHP CLI}} '''''PHP CLI Check if a process (program) is running''''' One the previous page I covered installing extension '''php_win32ps.dll''' which allows you to list statics fo...</p>
<p><b>New page</b></p><div>{{Uc nav PHP CLI}}<br />
'''''PHP CLI Check if a process (program) is running'''''<br />
<br />
One the previous page I covered installing extension '''php_win32ps.dll''' which allows you to list statics for all currently running processes. This page covers using the extension to check if a process is running strictly speaking this extension is not required. UniSerer already includes a small utility that performs a similar function it has the extra capability of killing a named process. For completeness both are covered allowing you to select the most suitable for your application.<br />
<br />
'''General note:''' ''Always consider alternatives however never be temped to use something that is OS version specific! A particular utility may not be included across the whole family range (XP,Vista etc) or may be renamed between OS versions. Always go for the lowest common denominator otherwise your scripts will be killed when run on another machine that is not running a compatible OS version.''<br />
== Initial test setup ==<br />
'''''Note'':''' ''It really is a shame; still unable to find a version of php_win32ps.dll that is compatible with PHP 5.3.0 hence for these examples I am assuming you are running one of the UniServer Mona series. At UniServer pskill.exe and the reset of this tutorial will switch back to using Uniform Server 5.0-Nano.''<br />
<br />
Edit our two test files '''Run.bat''' and '''test_1.php''' contained in folder UniServer to have the following content:<br />
{|<br />
|-<br />
|'''''Run.bat'''''||'''''test_1.php''''';<br />
|-valign="top"<br />
|<br />
<pre><br />
TITLE CLI TEST BAT<br />
COLOR B0<br />
@echo off<br />
cls<br />
echo.<br />
udrive\usr\local\php\php.exe test_1.php<br />
echo.<br />
pause<br />
</pre><br />
|<br />
<pre><br />
<?php<br />
print_r(win32_ps_list_procs());<br />
echo "Hello World";<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
|}<br />
'''''Note'':''' Above assumes you have installed the extension '''php_win32ps.dll''' as described on the [[PHP CLI: PHP INI#Extension download | previous page]].<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Extension '''''php_win32ps.dll''''' ==<br />
Run the batch file (double click on Run.bat), all current running processes are displayed along with some useful statistics.<br />
<br />
=== Reduce output ===<br />
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:<br />
<pre><br />
<?php<br />
//print_r(win32_ps_list_procs());<br />
<br />
@$processList = win32_ps_list_procs();<br />
foreach ($processList as $processArray)<br />
{<br />
$pid = $processArray['pid'];<br />
$exe = $processArray['exe'];<br />
echo "$pid $exe \n";<br />
}<br />
//echo "Hello World";<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
This produces a list of all running processes similar to this small extract:<br />
<pre><br />
3240 C:\Program Files\Mozilla Firefox\firefox.exe<br />
3660 C:\WINDOWS\System32\svchost.exe<br />
3796 F:\uc_media_wiki\Uniform Server\unicon.exe<br />
3640 Z:\usr\local\apache2\bin\Apache.exe<br />
3900 Z:\usr\local\mysql\bin\mysqld-opt.exe<br />
1396 Z:\usr\local\apache2\bin\Apache.exe<br />
2788 C:\Program Files\JGsoft\EditPadLite\EditPadLite.exe<br />
3484 C:\WINDOWS\system32\cmd.exe<br />
2988 C:\WINDOWS\system32\cmd.exe<br />
2916 H:\z_test_42\UniServer\udrive\usr\local\php\php.exe<br />
…… etc<br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Check a process is running ===<br />
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:<br />
<pre><br />
<?php<br />
$processName = 'cmd.exe';<br />
@$processList = win32_ps_list_procs();<br />
foreach ($processList as $processArray){<br />
$pid = $processArray['pid'];<br />
$exe = $processArray['exe'];<br />
if (basename($processArray['exe']) == $processName){<br />
echo "\n Process $processName is running\n";<br />
echo "$pid $exe \n";<br />
break;<br />
}<br />
}<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
Run the batch file (double click on Run.bat) output will be similar to this:<br />
<pre><br />
Process cmd.exe is running<br />
1896 C:\WINDOWS\system32\cmd.exe<br />
</pre><br />
The above can be converted into a function for general purpose however there is a small problem.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Problem ===<br />
I purposefully masked an issue with the above script. Comment out the break line as shown. <br />
{|<br />
|-valign="top"<br />
|<br />
<pre><br />
<?php<br />
$processName = 'cmd.exe';<br />
@$processList = win32_ps_list_procs();<br />
foreach ($processList as $processArray){<br />
$pid = $processArray['pid'];<br />
$exe = $processArray['exe'];<br />
if (basename($processArray['exe']) == $processName){<br />
echo "\n Process $processName is running\n";<br />
echo "$pid $exe \n";<br />
// break;<br />
}<br />
}<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
|<br />
Run the batch file (double click on Run.bat) several times<br />
<br />
Result similar to this><br />
<pre><br />
Process cmd.exe is running<br />
3860 C:\WINDOWS\system32\cmd.exe<br />
<br />
Process cmd.exe is running<br />
604 C:\WINDOWS\system32\cmd.exe<br />
<br />
Process cmd.exe is running<br />
3788 C:\WINDOWS\system32\cmd.exe<br />
</pre><br />
|}<br />
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.<br />
<br />
If you wish to avoid this issue and kill processes by name make sure each process has a '''unique name'''.<br />
<br />
Note:<br />
<br />
You may see only a single cmd process running, if that is the case open a few command prompts before running the script. <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Kill process by name ===<br />
This script uses a function to kill a named process, edit test_1.php and run the script (Run.bat)<br />
<pre><br />
<?php<br />
kill_proc("cmd.exe");<br />
<br />
function kill_proc($processName){<br />
@$processList = win32_ps_list_procs();<br />
foreach ($processList as $processArray){<br />
$pid = $processArray['pid'];<br />
if (basename($processArray['exe']) == $processName){ <br />
`taskkill /f /PID $pid 2>&1`; <br />
}<br />
}<br />
}<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
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:<br />
<pre><br />
<?php<br />
kill_proc("cmd.exe");<br />
<br />
function kill_proc($processName){<br />
@$processList = win32_ps_list_procs();<br />
foreach ($processList as $processArray){<br />
$pid = $processArray['pid'];<br />
if (basename($processArray['exe']) == $processName){ <br />
`tskill $pid `; <br />
}<br />
}<br />
}<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
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.<br />
<br />
That said I have not confirmed (by testing) if '''tskill''' works on XP-pro or Vista family. <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Clean up ==<br />
At this stage we have finished exploring extension '''php_win32ps.dll''' and have no further use for file '''php-cli.ini'''.<br />
<br />
I like to remove clutter if you feel the same delete the following:<br />
* Delete folder: UniServer\udrive\usr\local\php\'''new_extensions'''<br />
* Delete file: UniServer\udrive\usr\local\php\'''php-cli.ini'''<br />
* Pack away UniServer Mona<br />
* The remainder of this tutorial uses UniServer Nano<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== UniServer pskill.exe ==<br />
UniServer '''pskill.exe''' utility is battle harden, by this I mean, beta testers have proven its functionality across both XP and Vista families and beta release of Windows 7.<br />
<br />
It a dual function utility, can kill a named process or check if a named process is running.<br />
<br />
'''Note: From here on I am assuming you are using UniServer 5.0-Nano or above'''<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Initial test setup ===<br />
Edit our test file '''Run.bat''' contained in folder UniServer to have the following content:<br />
{|<br />
|-<br />
|'''''Run.bat'''''<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 />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Check a process is running ===<br />
Edit our test file '''test_1.php''' contained in folder UniServer to have the following content:<br />
{|<br />
|-<br />
|'''''test_1.php''''';<br />
|-valign="top"<br />
|<br />
<pre><br />
<?php<br />
$exe_name = "notepad.exe"; // process to check<br />
$command= "unicon\program\pskill.exe $exe_name"; // command line to be run <br />
exec($command,$dummy,$return); // 0=running 1=not-running <br />
<br />
if($return == 0){ // Check return value<br />
echo " $exe_name Is running";<br />
}<br />
else{<br />
echo " $exe_name Not running";<br />
}<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
|}<br />
<br />
'''''Test 1'':'''<br />
<br />
* Run Notepad<br />
* Run the batch file (double click on Run.bat), Process running displayed<br />
<br />
'''''Test 2'':'''<br />
<br />
* Close Notepad (and any other open Notepads)<br />
* Run the batch file (double click on Run.bat), Process Not running displayed<br />
<br />
'''''[[#top | Top]]'''''<br />
==== Function exec() ====<br />
This function exec($command,$dummy,$return) runs the utility pskill.exe and collects data returned.<br />
{|<br />
|-<br />
|* '''$command''' || - Command that will be executed. Path to program to run (pskill.exe) and any parameters to pass to it. <br />
|-<br />
|* '''$dummy''' || - Dummy array filled with every line of output from pskill. We ignore this data. Must have some variable here hence <br />
|-<br />
|* '''$return''' || - This variable holds returned error code from pskill. Named process running = 0 not running = 1 <br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Kill process by name ===<br />
Edit our test file '''test_1.php''' contained in folder UniServer to have the following content:<br />
{|<br />
|-<br />
|'''''test_1.php'''''||&nbsp;<br />
|-valign="top"<br />
|<br />
<pre><br />
<?php<br />
$exe_name = "notepad.exe"; // process to kill<br />
<br />
// command line to be run note c as second parameter kills<br />
// the above named process <br />
<br />
$command= "unicon\program\pskill.exe $exe_name c"; <br />
exec($command); // runs command <br />
exit(0); // Script ran OK<br />
?><br />
?><br />
</pre><br />
|<br />
<br /><br />
'''''Test'':'''<br />
* Run Notepad<br />
* Run the batch file (double click on Run.bat), Closes (kills) Notepad<br />
<br />
'''''Note'':'''<br />
<br />
Function exec($command) only a single parameter is required we are ignoring any returned data.<br />
|}<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
The real point of this part of the tutorial was the introduction to '''UniServer’s pskill.exe''' with its ability to check for a running process and if required to kill it. It kills by process name not by PID not a real issue if you have control of naming the process (program).<br />
<br />
Interestingly to build a fully functional CLI script you require several snippets of code that target problem areas. Consider this scenario, when a script starts another process it generally has to wait for that process to end before executing the next instruction.<br />
<br />
Not a problem if you are going to process data from that process; however if all you want to do is kick the process off and then continue, your script will appear to hang if the process takes a long time or never terminates.<br />
<br />
A solution to this scenario is covered on the [[PHP CLI: Detached Processes | '''next page''']].<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