note 57215 added to function.shell-exec

From: Date: Tue, 27 Sep 2005 13:00:59 +0000
Subject: note 57215 added to function.shell-exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-95873@lists.php.net to get a copy of this message
<?php /** * PHP Kill Process * * Sometimes, it can happen a script keeps running when it shouldn't, and it * won't stop after we close the browser, or shutdown the computer. Because it's * not always easy to use SSH there's a workaround. * * @author Jensen Somers <php@jsomers.be> * @version 1.0 */ class KillAllProcesses { /** * Construct the class */ function killallprocesses() { $this->listItems(); } /** * List all the items */ function listItems() { /* * PS Unix command to report process status * -x Select processes without controlling ttys * * Output will look like: * 16479 pts/13 S 0:00 -bash * 21944 pts/13 R 0:00 ps -x * */ $output = shell_exec('ps -x'); $this->output($output); // Put each individual line into an array $array = explode("\n", $output); $this->doKill($array); } /** * Print the process list * @param string $output */ function output($output) { print "<pre>".$output."</pre>"; } /** * Kill all the processes * It should be possible to filter in this, but I won't do it now. * @param array $array */ function doKill($array) { /* * Because the first line of our $output will look like * PID TTY STAT TIME COMMAND * we'll skip this one. */ for ($i = 1; $i < count($array); $i++) { $id = substr($array[$i], 0, strpos($array[$i], ' ?')); shell_exec('kill '.$id); } } } new KillAllProcesses(); ?> It's not the very best solution, but I've used it a couple of times when I needed to do it quick without to much trouble. Make not I kill all the processes, on my server px -x will only return like 4 times /sbin/apache and it's pretty safe to kill them without any trouble. ---- Manual Page -- http://www.php.net/manual/en/function.shell-exec.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57215 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57215&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57215&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57215&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57215&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57215&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57215&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57215&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57215&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95873) next »