PHPackages                             snailweb/php-daemon - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. snailweb/php-daemon

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

snailweb/php-daemon
===================

PHP Daemon

2.0.0(7y ago)028PHP

Since Mar 2Pushed 7y ago1 watchersCompare

[ Source](https://github.com/xavier-rodet/php-daemon)[ Packagist](https://packagist.org/packages/snailweb/php-daemon)[ RSS](/packages/snailweb-php-daemon/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

php-daemon
==========

[](#php-daemon)

PHP Daemon which can intercept signals to stop running properly.

Installation
------------

[](#installation)

`composer require snailweb/php-daemon`

Recommendations
---------------

[](#recommendations)

PHP is not the safer way to run process because of memory leaks, to avoid it this Daemon will automatically stop depending on his lifetime and memory usage.

That means you MUST use a service like [Supervisor](http://supervisord.org/) to run this daemon truly forever !

Usage
-----

[](#usage)

### Create your processor

[](#create-your-processor)

Create a Processor to accomplish your task

```
final class AdminNotifierProcessor implements \Snailweb\Daemon\Processor\ProcessorInterface
{
    private $dao;
    private $notifier;

    // Inject your dependencies
    public function __construct(DAO $dao, Notifier $notifier)
    {
        $this->dao = $dao;
        $this->notifier = $notifier;
    }

    // Initialize stuff
    public function setUp(): void
    {
        $this->dao->connect();
        $this->notifier->setEmail('admin@domain.com');
    }

    // CLear stuff
    public function tearDown(): void
    {
        $this->dao->disconnect();
        unset($this->dao, $this->notifier);
    }

    // Do your stuff
    public function process(): void
    {
        $results = $this->dao->query("SELECT id, message FROM notifications WHERE status='notify'");
        foreach ($results as $result) {
            $this->notifier->notify($result->message);
            $this->dao->query("UPDATE table SET status = 'notified' WHERE id = ?", $results->id);
        }
    }
}
```

### Basic Daemon usage :

[](#basic-daemon-usage-)

```
$processor = new AdminNotifierProcessor($dao, $notifier);

$daemon = new \Snailweb\Daemon\Daemon($processor);
$daemon->run();
```

By default daemon's strategy is to run forever however you can change his behaviour upon your needs.

### Other's Daemon usage :

[](#others-daemon-usage-)

Run daemon for 5 process iterations

```
$strategy = new Snailweb\Daemon\Strategy\Iteration(5);
$daemon->run($strategy);
```

Run daemon for 1 minute

```
$strategy = new Snailweb\Daemon\Strategy\Timer(60);
$daemon->run($strategy);
```

You can easily create your own Strategy by extending `\Snailweb\Daemon\Strategy\AbstractStrategy`

### Configure the daemon

[](#configure-the-daemon)

The default daemon configuration can be override

```
$daemon->setOptions([
    'run_ttl' => 86400, // the daemon will stop after 1 day of runtime
    'run_memory_limit' => 128, // the daemon will stop when he reached 128MB of memory usage
    'process_min_exec_time' => 100, // The minimum time between 2 process execution (to avoid CPU overload when your process has nothing do)
]);
```

Note: you don't have to set all options, just the ones you want to override :)

### Handle signals

[](#handle-signals)

This daemon allow you to handle [UNIX signals](https://en.wikipedia.org/wiki/Signal_(IPC)#POSIX_signals) to change his behaviour.

Create your SignalHandler

```
final class MySignalHandler extends \Snailweb\Daemon\Signals\Handler\AbstractSignalsHandler
{
    public function handle(int $signal, Snailweb\Daemon\Daemon $daemon): void
    {
        switch ($signal) {
            case SIGINT:

                // Change daemon's behaviour

                // Apply an other processor
                $daemon->setProcessor(new ProperExitProcessor());
                // For only 10 more iterations
                $daemon->setStrategy(new \Snailweb\Daemon\Strategy\Iteration(10));

                break;
            case SIGTERM:

                // Instantly stop our script
                $daemon->stop();

                break;
        }
    }
}
```

Apply the signal handler to your daemon :

```
$processor = new AdminNotifierProcessor();

$signals = new \Snailweb\Daemon\Signals\Signals([SIGINT, SIGTERM]);
$signalsListener = new \Snailweb\Daemon\Signals\Listener\SignalsListener();
$signalsHandler = new MySignalHandler();
$signalsManager = new \Snailweb\Daemon\Signals\Manager\SignalsManager($signals, $signalsListener, $signalsHandler);

$daemon = new \Snailweb\Daemon\Daemon($processor, $signalsManager);
$daemon->run();
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

3

Last Release

2625d ago

Major Versions

1.0.1 → 2.0.02019-03-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/d517fd68c868e68f4d47de1b46db8046495d44cee43e79fc9635e35218ae5657?d=identicon)[xavier-rodet](/maintainers/xavier-rodet)

---

Top Contributors

[![xavier-rodet](https://avatars.githubusercontent.com/u/2737842?v=4)](https://github.com/xavier-rodet "xavier-rodet (27 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/snailweb-php-daemon/health.svg)

```
[![Health](https://phpackages.com/badges/snailweb-php-daemon/health.svg)](https://phpackages.com/packages/snailweb-php-daemon)
```

###  Alternatives

[jacopo/bootstrap-3-table-generator

Dynamic table generator for bootstrap 3

1411.5k1](/packages/jacopo-bootstrap-3-table-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
