2017 © Pedro Peláez
 

library background-process

Start processes in the background that continue running when the PHP process exists.

image

cocur/background-process

Start processes in the background that continue running when the PHP process exists.

  • Saturday, February 11, 2017
  • by florianeckerstorfer
  • Repository
  • 16 Watchers
  • 200 Stars
  • 59,830 Installations
  • PHP
  • 9 Dependents
  • 0 Suggesters
  • 47 Forks
  • 7 Open issues
  • 12 Versions
  • 16 % Grown

The README.md

cocur/background-process

Start processes in the background that continue running when the PHP process exists., (*1)

Latest Stable Version Build Status Windows Build status Code Coverage, (*2)

Installation

You can install Cocur\BackgroundProcess using Composer:, (*3)

$ composer require cocur/background-process

Usage

The following example will execute the command sleep 5 in the background. Thus, if you run the following script either in the browser or in the command line it will finish executing instantly., (*4)

use Cocur\BackgroundProcess\BackgroundProcess;

$process = new BackgroundProcess('sleep 5');
$process->run();

You can retrieve the process ID (PID) of the process and check if it's running:, (*5)

use Cocur\BackgroundProcess\BackgroundProcess;

$process = new BackgroundProcess('sleep 5');
$process->run();

echo sprintf('Crunching numbers in process %d', $process->getPid());
while ($process->isRunning()) {
    echo '.';
    sleep(1);
}
echo "\nDone.\n";

If the process runs you can stop it:, (*6)

// ...
if ($process->isRunning()) {
    $process->stop();
}

Please note: If the parent process continues to run while the child process(es) run(s) in the background you should use a more robust solution, for example, the Symfony Process component., (*7)

Windows Support

Since Version 0.5 Cocur\BackgroundProcess has basic support for Windows included. However, support is very limited at this time. You can run processes in the background, but it is not possible to direct the output into a file and you can not retrieve the process ID (PID), check if a process is running and stop a running process., (*8)

In practice, the following methods will throw an exception if called on a Windows system:, (*9)

  • Cocur\BackgroundProcess\BackgroundProcess::getPid()
  • Cocur\BackgroundProcess\BackgroundProcess::isRunning()
  • Cocur\BackgroundProcess\BackgroundProcess::stop()

Create with existing PID

If you have a long running process and store its PID in the database you might want to check at a later point (when you don't have the BackgroundProcess object anymore) whether the process is still running and stop the process., (*10)

use Cocur\BackgroundProcess\BackgroundProcess;

$process = BackgroundProcess::createFromPID($pid);
$process->isRunning(); // -> true
$process->stop();      // -> true

Change Log

Version 0.7 (11 February 2017)

Version 0.6 (10 July 2016)

  • #17 Add ability to append to file on Unix/Linux-based systems (by bpolaszek)

Version 0.5 (24 October 2015)

  • Added basic support for Windows

Version 0.4 (2 April 2014)

  • Moved repository to Cocur organization
  • Changed namespace to Cocur
  • PSR-4 compatible namespace
  • #3 Added BackgroundProcess::stop() (by florianeckerstorfer)

Version 0.3 (15 November 2013)

  • Changed namespace to Braincrafted

Author

Florian Eckerstorfer, (*11)

License

The MIT license applies to cocur/background-process. For the full copyright and license information, please view the LICENSE file distributed with this source code., (*12)

The Versions

11/02 2017

0.7.x-dev

0.7.9999999.9999999-dev

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

process background unix

11/02 2017

dev-master

9999999-dev

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

process background unix

11/02 2017

v0.7

0.7.0.0

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

process background unix

10/07 2016

0.6.x-dev

0.6.9999999.9999999-dev

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

process background unix

10/07 2016

v0.6

0.6.0.0

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

process background unix

24/10 2015

0.5.x-dev

0.5.9999999.9999999-dev

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Development Requires

process background unix

24/10 2015

v0.5

0.5.0.0

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Development Requires

process background unix

02/05 2014

v0.4

0.4.0.0

Start processes in the background that continue running when the PHP process exists.

  Sources   Download

MIT

The Development Requires

process background unix

15/11 2013

0.3.x-dev

0.3.9999999.9999999-dev

  Sources   Download

The Development Requires

15/11 2013

v0.3

0.3.0.0

  Sources   Download

The Development Requires

24/04 2013

v0.2

0.2.0.0

  Sources   Download

The Development Requires

24/04 2013

v0.1

0.1.0.0

  Sources   Download

The Development Requires