note 90584 added to function.proc-open

From: Date: Wed, 29 Apr 2009 14:14:10 +0000
Subject: note 90584 added to function.proc-open
Groups: php.notes 
Request: Send a blank email to php-notes+get-154001@lists.php.net to get a copy of this message
Hi, to start processes under win and continue with the script while being able to further manage these processes using their PID, i have written three small functions that use proc_open together with three tools from the MS sysinternals suite (psexec.exe, pslist.exe, pskill.exe) (NB: the sysinternal tools work as standalone, but need some registry additions, otherwise they pop open an EULA window on first run, see below) string proc_start ( string $command ) -> starts an independent process using $command on the command shell and returns its PID when successful or else FALSE boolean proc_isalive ( string $pid ) -> returns TRUE if the process with PID provided is running, otherwise FALSE boolean proc_kill ( string $pid ) -> tries to kill the process with PID provided and returns TRUE if successful The sysinternals tools are here:: http://technet.microsoft.com/en-us/sysinternals/ The registry settings that need be added: ---START OF REG FILE--- Windows Registry Editor Version 5.00 [HKEY_USERS\.DEFAULT\Software\Sysinternals] [HKEY_USERS\.DEFAULT\Software\Sysinternals\PsExec] "EulaAccepted"=dword:00000001 [HKEY_USERS\.DEFAULT\Software\Sysinternals\PsList] "EulaAccepted"=dword:00000001 [HKEY_USERS\.DEFAULT\Software\Sysinternals\PsKill] "EulaAccepted"=dword:00000001 ---EOF--- The code: <?php /********************************************************************/ /********************************************************************/ function start_proc ( $comm ) { $dn=dirname(__FILE__); $descriptorspec = array( 0 => array('pipe', 'r'), // stdin 1 => array('pipe', 'w'), // stdout 2 => array('pipe', 'w') // stderr ); $fpr = proc_open('psexec.exe -d '.$comm, $descriptorspec, $pipes, $dn); fclose($pipes[0]); fclose($pipes[1]); $stderr = ''; while(!feof($pipes[2])) { $stderr .= fgets($pipes[2], 128); } fclose($pipes[2]); proc_close ($fpr); $pid =FALSE; if ( preg_match ( "/process ID ([\d]{1,10})\./im", $stderr, $matches ) ) $pid = $matches[1]; else $pid=FALSE; return $pid; } /********************************************************************/ /********************************************************************/ function proc_isalive ( $pid ){ $alive=FALSE; $dn=dirname(__FILE__); $descriptorspec = array( 0 => array('pipe', 'r'), // stdin 1 => array('pipe', 'w'), // stdout 2 => array('pipe', 'w') // stderr ); $fpr = proc_open( 'pslist.exe '.$pid, $descriptorspec, $pipes, $dn ); fclose($pipes[0]); $stdout = ''; while(!feof($pipes[1])) { $stdout .= fgets($pipes[1], 128); } fclose($pipes[1]); $stderr = ''; while(!feof($pipes[2])) { $stderr .= fgets($pipes[2], 128); } fclose($pipes[2]); proc_close ($fpr); if ( strpos($stdout, 'not found') === FALSE ) $alive=TRUE; return $alive; } /********************************************************************/ /********************************************************************/ function proc_kill ( $pid ){ $succ=FALSE; $dn=dirname(__FILE__); $descriptorspec = array( 0 => array('pipe', 'r'), // stdin 1 => array('pipe', 'w'), // stdout 2 => array('pipe', 'w') // stderr ); $fpr = proc_open( 'pskill.exe '.$pid, $descriptorspec, $pipes, $dn ); fclose($pipes[0]); $stdout = ''; while(!feof($pipes[1])) { $stdout .= fgets($pipes[1], 128); } fclose($pipes[1]); $stderr = ''; while(!feof($pipes[2])) { $stderr .= fgets($pipes[2], 128); } fclose($pipes[2]); proc_close ($fpr); if ( strpos($stdout, 'killed') !== FALSE ) $succ=TRUE; return $succ; } /********************************************************************/ /********************************************************************/ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 196.14.186.18 ---- Manual Page -- http://www.php.net/manual/en/function.proc-open.php Edit -- https://master.php.net/note/edit/90584 Del: integrated -- https://master.php.net/note/delete/90584/integrated Del: useless -- https://master.php.net/note/delete/90584/useless Del: bad code -- https://master.php.net/note/delete/90584/bad+code Del: spam -- https://master.php.net/note/delete/90584/spam Del: non-english -- https://master.php.net/note/delete/90584/non-english Del: in docs -- https://master.php.net/note/delete/90584/in+docs Del: other reasons-- https://master.php.net/note/delete/90584 Reject -- https://master.php.net/note/reject/90584 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154001) next »