PHPackages                             rogerthomas84/daemoniser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. rogerthomas84/daemoniser

ActiveLibrary

rogerthomas84/daemoniser
========================

Daemoniser is a PHP library to help make daemon applications

1.1.5(6y ago)015MITPHPPHP &gt;=5.0

Since Dec 3Pushed 6y ago1 watchersCompare

[ Source](https://github.com/rogerthomas84/daemoniser)[ Packagist](https://packagist.org/packages/rogerthomas84/daemoniser)[ Docs](https://github.com/rogerthomas84/daemoniser)[ RSS](/packages/rogerthomas84-daemoniser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (10)Used By (0)

```
 __ \                                  _)
 |   |  _` |  _ \ __ `__ \   _ \  __ \  |  __|  _ \  __|
 |   | (   |  __/ |   |   | (   | |   | |\__ \  __/ |
____/ \__,_|\___|_|  _|  _|\___/ _|  _|_|____/\___|_|

```

What is it
----------

[](#what-is-it)

Daemoniser is a PHP library to take the stress off of creating PHP based daemons. If you're questioning why then this library isn't for you. Generally PHP shouldn't be used for creating daemons.

Use it with Composer
--------------------

[](#use-it-with-composer)

`$ composer require rogerthomas84/daemoniser`

How to use
----------

[](#how-to-use)

For help writing your first daemon, see the example in `example/example.php`.

A daemon requires an instance of `DaemonConfig` to be passed in via the `execute` method.

DaemonConfig constructor:
-------------------------

[](#daemonconfig-constructor)

1. `DaemonAbstract $damon` / The instance of your daemon (which extends `DaemonAbstract`)
    - Required
2. `$errorLogFilePath` / A full path to either a new or existing log file to use for logging errors.
    - Optional, defaults to `tmp/log/error_My_Class_Name.log` (assuming fully qualified class name is `\My\Class\Name`)
3. `$infoLogFilePath` / A full path to either a new or existing log file to use for logging echo'd content.
    - Optional, defaults to `tmp/log/info_My_Class_Name.log` (assuming fully qualified class name is `\My\Class\Name`)
4. `$pidFilePath` / A full path to either a new or existing file to use for storing the pid of the dameon'd instance.
    - Optional, defaults to `tmp/pid/My_Class_Name.pid` (assuming fully qualified class name is `\My\Class\Name`)
5. `$softStopFilePath` / The path to the soft stop file.
    - Optional, defaults to `tmp/stops/stop_My_Class_Name.log` (assuming fully qualified class name is `\My\Class\Name`)
6. `$sleepDuration` / How long to sleep after each call to your `run()` method.
    - Optional, defaults to `5` seconds
7. `$maxLogFileSize` / How large in bytes should your log file be allowed to get before
    - Optional, defaults to `100000000` bytes, (100 MB)
8. `$iniSettings` / A key =&gt; value array of settings to use with `ini_set`. For example, `['memory_limit' => '1024MB']`
    - Optional, defaults to empty array
9. `$whitelistedUsers` / An array containing the usernames allowed to execute this command.
    - Optional, defaults to empty array, allowing any user.

**All config options are also available `set` and `get` methods in the config object.**

Usage
-----

[](#usage)

Daemoniser has several helpful commands.

1. `php my-daemon.php status` - Get the status of the daemon
2. `php my-daemon.php start` - Start the daemon
3. `php my-daemon.php soft-stop` - Stop the daemon via a stop file gracefully.
4. `php my-daemon.php stop` - Stop the daemon immediately (this is not ideal)
5. `php my-daemon.php restart` - Restart the daemon (this is not ideal)
6. `php my-daemon.php pid` - Get the PID for the daemon
7. `php my-daemon.php rm-logs` - Delete historic logs for this daemon.
8. `php my-daemon.php rm-logs all` - Delete **ALL** logs for this daemon.
9. `php my-daemon.php help` - Show the help content

You shouldn't ever use `stop` really. Doing so will result in the process being immediately killed. But this is less of a problem if you aren't running something that could result in data loss in the case of an immediate halt being called.

If you 'must' have an immediate stop, you'll have to implement the `canImmediatelyStop()` method in your daemons and return `true`. This should be avoided at all costs, as the stop process simply kills the `pid` immediately.

Extending the commands:
-----------------------

[](#extending-the-commands)

You can easily extend the commands to perform different actions outside of the `run()` loop. To do this, simply implement the `protected function getAdditionalCommands()` method.

You'd need to return an array of objects. Each object being an instance of `DaemonCommand`. There's a helpful `::build()` method available on the `DaemonCommand` which you can use to easily build new functionality.

Should I even use this?
-----------------------

[](#should-i-even-use-this)

If you're asking yourself this question, then the answer is no. No you shouldn't. This library fills very specific requirements.

Full example file:
------------------

[](#full-example-file)

```
