PHP Classes
elePHPant
Icontem

PID process check: Check if a PHP script is running using PID files

Recommend this page to a friend!
  Info   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2015-04-02 (2 years ago) RSS 2.0 feedNot enough user ratingsTotal: 437 All time: 6,018 This week: 778Up
Version License PHP version Categories
pid-process-check 2.0.5BSD License5.3.0PHP 5, Unix, Console, Windows
Description Author

This class can check if a PHP script is running using PID files.

It looks at a given directory to see if it exists a process identifier file with the number of the current script process.

If the file exists, the class can call a Windows COM object or send a POSIX signal under a Linux/Unix system to determine if a process is running.

If the script is not yet running, the class creates the PID file and stores process number in it.

  Performance   Level  
Name: Camilo Sperberg <contact>
Classes: 8 packages by
Country: The Netherlands The Netherlands
Age: 35
All time rank: 75623 in The Netherlands The Netherlands
Week rank: 317 Up10 in The Netherlands The Netherlands Up

Details
[![Latest Stable Version](https://poser.pugx.org/unreal4u/pid/v/stable.png)](https://packagist.org/packages/unreal4u/pid)
[![Build Status](https://travis-ci.org/unreal4u/pid.png?branch=master)](https://travis-ci.org/unreal4u/pid)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/unreal4u/pid/badges/quality-score.png?s=250617550b830844374c830e955dfbdd31df3c11)](https://scrutinizer-ci.com/g/unreal4u/pid/)
[![Code Coverage](https://scrutinizer-ci.com/g/unreal4u/pid/badges/coverage.png?s=69f58ff3d306565bcde70c045878420f7bbdbd29)](https://scrutinizer-ci.com/g/unreal4u/pid/)
[![License](https://poser.pugx.org/unreal4u/pid/license.png)](https://packagist.org/packages/unreal4u/pid)

pid.php
======

Credits
--------

This class is made by unreal4u (Camilo Sperberg). [unreal4u.com/](http://unreal4u.com).

About this class
--------

* Can be used to verify whether a process is already running or not.
* Is platform independant: Can be used in Windows or Linux. In both, they will call OS specific functions to find out whether the process is running or not.
* It does not detect previous fatal errors, but it can omit the previous PID file if a given time has passed since the creation.

Detailed description
---------

This package will check if a certain PID file is present or not, and depending on that will:

Create a PID file.
If it already exists, will ask the OS to check whether it is still a running process.
If for whatever reason, the OS still thinks the process is still running and too much time has passed, the class can overwrite the previous PID file (Thus initiating a new instance).
When the object is destroyed, the corresponding PID file will be deleted as well.

Basic usage
----------

<pre>include('src/unreal4u/pid.php');
try {
    $pid = new unreal4u\pid();
} catch (\Exception $e) {
    echo $e->getMessage();
}

if ($pid->isAlreadyRunning) {
    echo 'Your process is already running';
}
</pre>
* `$pid->pid` will show you the pid number.
* **Please see examples for more options and advanced usage**
* There is only one caveat: if you are going to use this class inside a method within a class, ensure that the destructor gets executed when it should: variables are immediatly destroyed after the method finishes executing, so the PID will be destroyed as well. To ensure this, assign the PID class to an object inside the class, that way, whenever that object gets destroyed, this class will be as well.

Composer
----------

This class has support for (preferably) Composer install. Just add the following section to your composer.json with:

<pre>
{
    "require": {
        "unreal4u/pid": "@stable"
    }
}
</pre>

Now you can instantiate a new pid class by executing:

<pre>
require('vendor/autoload.php');

try {
    $pid = new unreal4u\pid();
} catch (\Exception $e) {
    // Do something
}
</pre>

Pending
---------
* Better (more thorough) code coverage on PHPUnit tests.
* Test this class thoroughly on a windows machine, many UNIX improvements has been made in the meantime.

Version History
----------

* 1.0 :
    * Initial version
* 1.1:
    * Support for Windows PID check
* 1.3:
    * PHPUnit testing
    * Documentation improved (Created this README actually)
    * More examples
* 1.3.1:
    * Script now set itself a maximum execution time
* 1.4.0:
    * Class is now compatible with composer
* 1.4.2:
    * Better documentation
    * Better code coverage
* 1.4.5:
    * Travis-CI support
    * Began deprecating old coding standard
* 2.0.0:
    * Mayor rewrite of basic functioning of the class
    * Variables can now be passed on as an array instead of per variable
    * Class now throws (more) exceptions when something went wrong
    * More tests regarding new functionality
    * Backwards compatibility changes:
        * Class will now throw exceptions when it fails at some part instead of silently failing
        * Function <code>getTSpidFile()</code> renamed to <code>getTimestampPidFile()</code>

Contact the author
-------

* Twitter:   [@unreal4u](http://twitter.com/unreal4u)
* Website:   [http://unreal4u.com/](http://unreal4u.com/)
* Github:    [http://www.github.com/unreal4u](http://www.github.com/unreal4u)
* Packagist: [https://packagist.org/users/unreal4u/]
  Files folder image Files  
File Role Description
Files folder imageexamples (5 files)
Files folder imagesrc (1 directory)
Files folder imagetests (1 file)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file complex-example.php Example Example script
  Accessible without login Plain text file job-with-fatal-error.php Example Example script
  Accessible without login Plain text file longRunningFunction.php Example Example script
  Accessible without login Plain text file simple-example.php Example Example script
  Accessible without login Plain text file stale-process.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageunreal4u (2 files)

  Files folder image Files  /  src  /  unreal4u  
File Role Description
  Accessible without login Plain text file exceptions.php Aux. Auxiliary script
  Accessible without login Plain text file pid.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file pidTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:437
This week:0
All time:6,018
This week:778Up