Jump to content

php exec() get PID

Go to solution Solved by mikebald,

If this is a linux based machine you can use the class made by dell_petter at hotmail dot com found at http://php.net/manual/en/function.exec.php:

<?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);    // 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;    }}?>

If you're not on linux, I would look into proc_open.

Super simple question.

 

I am running a java program with the PHP "exec()" command, that needs manually killed at a given time. Normally, i would issue the "pkill java" command, but that would kill all instances where i only need the specific one i called earlier killed.

 

I cant seem to find online where to get the PID, anyone know anything?

~Judah

Link to comment
https://linustechtips.com/topic/267399-php-exec-get-pid/
Share on other sites

Link to post
Share on other sites

------------------------------------

     ~ Live Love Code ~

------------------------------------

Link to comment
https://linustechtips.com/topic/267399-php-exec-get-pid/#findComment-3632873
Share on other sites

Link to post
Share on other sites

If this is a linux based machine you can use the class made by dell_petter at hotmail dot com found at http://php.net/manual/en/function.exec.php:

<?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);    // 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;    }}?>

If you're not on linux, I would look into proc_open.

Link to comment
https://linustechtips.com/topic/267399-php-exec-get-pid/#findComment-3633683
Share on other sites

Link to post
Share on other sites

This is awesome, ill look into it.

 

Thanks

If this is a linux based machine you can use the class made by dell_petter at hotmail dot com found at http://php.net/manual/en/function.exec.php:

<?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);    // 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;    }}?>

If you're not on linux, I would look into proc_open.

~Judah

Link to comment
https://linustechtips.com/topic/267399-php-exec-get-pid/#findComment-3636571
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×