Process is a PHP package for managing external executable processes.
$ composer require marando/process
New processes can be initialized by passing the process you wish to run to the first argument of the constructor:
$proc = new Process('ping google.com');
The process will not start until you call the start()
method:
$proc = new Process('ping google.com');
$proc->start();
If you wish to prematurely kill a process use the kill()
method:
$proc = new Process('ping google.com');
$proc->start();
$proc->kill();
You can check if a process is currently running by using the isRunning()
method:
$proc = new Process('ping google.com');
$proc->start();
$proc->isRunning();
If you need to know the PID of the process, use the pid
property:
$proc = new Process('ping google.com');
$proc->start();
$proc->pid;
The wait()
method will hold up code execution until the process has completed:
$proc = new Process('ping google.com');
$proc->start();
$proc->wait();
You can also get the output of the process from the log
property, which will return a PHP SplFileObject
instance that contains the output of the process. To enable this behavior you must first pass the path of the desired output file to the constructor:
$proc = new Process('ping google.com', 'logfile.log');
$proc->start();
$proc->wait();
// Echo first 100 characters of the output.
echo $proc->log->fread(100);