Processes

Introduction

Pletfix Process class starts a backround process using PHP's [proc_open] (http://php.net/manual/en/function.proc-open.php).

Create a Process Instance

You may use the process() function to create a process:

$process = process('ls - l');

You could set environment variables, e.g. like this:

$env = config('app.debug') ? ['XDEBUG_CONFIG' => 'idekey=PHPSTORM'] : null;

$process = process($cmd, $env);   

Of course, you can redirect the output in the conventional way, e.g. into a file:

$process = process($cmd . ' > output.txt 2>&1');

Or to Nirvana:

$process = process($cmd . ' > /dev/null 2>&1');

If you still want the process to run in the background after the HTTP request is completed, add a &:

$process = process($cmd . ' > /dev/null 2>&1 &');

Available Methods

Method Listing

run()

The run method runs a process:

$exitcode = $process->run();

You may set a timeout in seconds to wait until the process is finished:

$exitcode = $process->run(60);

Note, that the command sequence below are the same as $process->run():

$process->start();
$exitcode = $process->wait();

start()

The start method starts a background process:

$process->start();

wait()

The wait method waits for the process to terminate:

$exitcode = $process->wait();

You may set a timeout in seconds or null to disable:

$exitcode = $process->wait(60);

terminate()

The terminate method terminates the process:

$process->terminate();

This optional parameter is only useful on POSIX operating systems; you may specify a signal to send to the process using the kill(2) system call. The default is SIGTERM (15).

SIGTERM (15) is the termination signal. The default behavior is to terminate the process, but it also can be caught or ignored. The intention is to kill the process, but to first allow it a chance to cleanup.

SIGKILL (9) is the kill signal. The only behavior is to kill the process, immediately. As the process cannot catch the signal, it cannot cleanup, and thus this is a signal of last resort.

$process->terminate(SIGKILL);

kill()

The kill method kills the process immediately. It's a short way to invoke $process->terminate(SIGKILL):

$process->kill();

read()

The read method reads a line from STDOUT:

$line = $process->read();

write()

The write method writes a line to STDIN:

$process->write($string);

getOutput()

The getOutput method gets the output from STDOUT when the process is finished:

$output = $process->getOutput();

getErrorOutput()

The getErrorOutput method gets the error output from STDERR when the process is finished:

$error = $process->getErrorOutput();    

isRunning()

The isRunning method determines if the process is currently running:

$process->isRunning();

getExitCode()

The getExitCode method gets the exit code returned by the process:

$exitcode = $process->getExitCode();

isSuccessful()

The isSuccessful method determines if the process ended successfully:

$ok = $process->isSuccessful();

(edit on GitHub)