note 88704 added to function.exec

From: Date: Wed, 04 Feb 2009 10:52:04 +0000
Subject: note 88704 added to function.exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-150194@lists.php.net to get a copy of this message
(This is for linux users only). We know now how we can fork a process in linux with the & operator. And by using command: nohup MY_COMMAND > /dev/null 2>&1 & echo $! we can return the pid of the process. This small class is made so you can keep in track of your created processes ( meaning start/stop/status ). You may use it to start a process or join an exisiting PID process. <?php // You may use status(), start(), and stop(). notice that start() method gets called automatically one time. $process = new Process('ls -al'); // or if you got the pid, however here only the status() metod will work. $process = new Process(); $process.setPid(my_pid); ?> <?php // Then you can start/stop/ check status of the job. $process.stop(); $process.start(); if ($process.status()){ echo "The process is currently running"; }else{ echo "The process is not running."; } ?> <?php /* An easy way to keep in track of external processes. * Ever wanted to execute a process in php, but you still wanted to have somewhat controll of the process ? Well.. This is a way of doing it. * @compability: Linux only. (Windows does not work). * @author: Peec */ class Process{ private $pid; private $command; public function __construct($cl=false){ if ($cl != false){ $this->command = $cl; $this->runCom(); } } private function runCom(){ $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!'; exec($command ,$op); $this->pid = (int)$op[0]; } public function setPid($pid){ $this->pid = $pid; } public function getPid(){ return $this->pid; } public function status(){ $command = 'ps -p '.$this->pid; exec($command,$op); if (!isset($op[1]))return false; else return true; } public function start(){ if ($this->command != '')$this->runCom(); else return true; } public function stop(){ $command = 'kill '.$this->pid; exec($command); if ($this->status() == false)return true; else return false; } } ?> ---- Server IP: 128.39.198.38 Probable Submitter: 129.177.10.233 ---- Manual Page -- http://www.php.net/manual/en/function.exec.php Edit -- https://master.php.net/note/edit/88704 Del: integrated -- https://master.php.net/note/delete/88704/integrated Del: useless -- https://master.php.net/note/delete/88704/useless Del: bad code -- https://master.php.net/note/delete/88704/bad+code Del: spam -- https://master.php.net/note/delete/88704/spam Del: non-english -- https://master.php.net/note/delete/88704/non-english Del: in docs -- https://master.php.net/note/delete/88704/in+docs Del: other reasons-- https://master.php.net/note/delete/88704 Reject -- https://master.php.net/note/reject/88704 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150194) next »