PHPackages                             lifo/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. lifo/php-daemon

ActiveLibrary

lifo/php-daemon
===============

PHP Multiprocessing Daemon

v2.0.1(3y ago)5134.4k↓46.7%15MITPHPPHP &gt;=7.4

Since Jan 17Pushed 5mo ago14 watchersCompare

[ Source](https://github.com/lifo101/php-daemon)[ Packagist](https://packagist.org/packages/lifo/php-daemon)[ RSS](/packages/lifo-php-daemon/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (6)Used By (0)

PHP Daemon Library
------------------

[](#php-daemon-library)

### Synopsis

[](#synopsis)

Create robust and stable PHP multiprocess daemons without the boilerplate code. The core [Daemon](../../wiki/Daemon)class handles the main loop and events and can run at any frequency desired *(within the limits of PHP)*. You only have to implement a single method `execute` to run a daemon process, optionally in the background.

Using [Tasks](../../wiki/Tasks) and [Workers](../../wiki/Workers) the daemon can call methods on background processes seamlessly w/o worrying about managing forked children. [Plugins](../../wiki/Plugins) allow you to easily create reusable and shareable code for your Daemons. See the [Features](#features) section below for more information.

### Why write a daemon in PHP?

[](#why-write-a-daemon-in-php)

Obviously, writing robust, stable and long-running daemons in PHP is generally not a good idea. It's at least very hard to do, and do well. I personally needed a daemon in PHP because I had an entire website framework built in Symfony that needed a major back-end daemon. I wanted to be able to re-use all my front-end dependencies and entities w/o duplicating resources or configs.

While this library does everything it can to allow you to create a rock solid daemon, care must still be taken in your user-land code to keep things stable.

### Requirements

[](#requirements)

- PHP ^7.4 || ^8.0 *(for PHP ^5.4 use the ^1.0 tag and branch)*
- A POSIX compatible operating system (Linux, OSX, BSD)
- PHP [POSIX](http://php.net/posix) and [PCNTL](http://php.net/pcntl) Extensions

### Documentation

[](#documentation)

See the [Wiki](../../wiki) for documentation.

### Examples

[](#examples)

See the [examples](examples) directory for examples you can run.

### Features

[](#features)

- The `Main Loop` is maintained by the core [Daemon](../../wiki/Daemon) class. All you have to do is implement one method `execute` that will get called every loop cycle. The loop frequency can be any fractional value in seconds. If set to 0, your `execute` method will get called as fast as possible (*not normally recommended, unless your loop is doing some sort of blocking call, ie: listening on a socket, etc*).
- In just a few lines of code you can have parallel processes running in the background.
    - A [Task](../../wiki/Tasks) allows you to call any method or callback in a background process. No communication is made between the background process and the parent. Tasks are meant for simple things, for example: Sending an email.
    - A [Worker](../../wiki/Workers) allows you to call any method on an object, or even just a simple callback like a [Task](../../wiki/Tasks). Workers can return a value back to the parent via a simple `return` statement in your worker method(s). Workers are maintained automatically and can have multiple children running at the same time, which is handled transparently. Even if a worker dies or is killed by the OS the Daemon API will still return a result (or exception) to your code. The return value of a Worker is usually a `Promise` object. You can use the standard Promise methods like `then` or `otherwise` to act on the return value. Or you can register an `ON_RETURN`callback on the Worker.

        Workers use a [Mediator design pattern](https://en.wikipedia.org/wiki/Mediator_pattern) and use Shared Memory for it's messaging queue and data. Different IPC classes can be created to provide alternate communication methods between the parent and children. *I might work on a second IPC class that uses sockets instead of SHM to provide an alternate choice*.
- Event Handling. The core `Daemon` has several events (see: [Events](../../wiki/Events)) that you can easily interface with by registering a callback. Some events have the means to change the behavior of the daemon.
- Easy Signal Handling via the Event Dispatcher. To catch a signal you simply have to register a `ON_SIGNAL` callback in your code. Your callback will be passed an `SignalEvent` with the signal that was caught.
- Simple `Plugin` architecture allows you to use and create your own plugins that can be injected into the Daemon. Plugins can be lazily loaded.
    - A core plugin `FileLock` allows you to add a locking mechanism to prevent your daemon from running more than one instance at a time. Simply register the plugin in your daemon and the rest is automatic. A `ShmLock` is similar but uses Shared Memory to obtain a lock.
- Automatic restarting. The Daemon can automatically restart itself if it's runtime reached a configurable threshold or if a fatal error occurred.
- Built in logging. The `Daemon` has 3 basic logging methods: `log`, `error`, `debug`. All of these will write to the log file (if configured). If the log file is rotated, overwritten or deleted, the daemon will automatically detect this and will continue to write to the new log file. The [DaemonEvent::ON\_LOG](../../wiki/Events#on_log) event allows you to register a callback to change the behavior too. User code can use the [LogTrait](../../wiki/Logging) to easily add native daemon logging to their code.

### Credit

[](#credit)

The basis for this library was inspired by the [PHP-Daemon](https://github.com/shaneharter/PHP-Daemon) library from [Shane Harter](https://github.com/shaneharter) on GitHub. Unfortunately, his library was abandoned (or is on indefinite hiatus), was written for PHP v5.3, had no namespacing, no package management or an auto-loader (ie: Composer).

I choose to create an entirely new library instead of forking and modifying his original library for educational purposes. I also didn't agree with some of his methodologies. I do require some extra dependencies, but [Composer](http://getcomposer.org/) makes this a trivial issue.

---

\_This library is in a fully working state. I've created very complex daemons that have run for months w/o any memory leaks or crashes. More could be done...

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance49

Moderate activity, may be stable

Popularity42

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 97.3% 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 ~453 days

Total

5

Last Release

1231d ago

Major Versions

1.0.x-dev → v2.02022-11-14

PHP version history (2 changes)v1.0PHP &gt;=5.4.4

v2.0PHP &gt;=7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/367337?v=4)[Jason M](/maintainers/lifo101)[@lifo101](https://github.com/lifo101)

---

Top Contributors

[![lifo101](https://avatars.githubusercontent.com/u/367337?v=4)](https://github.com/lifo101 "lifo101 (36 commits)")[![bl-lfl](https://avatars.githubusercontent.com/u/222427138?v=4)](https://github.com/bl-lfl "bl-lfl (1 commits)")

---

Tags

daemonphp-daemonphp-librarysymfony

### Embed Badge

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

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

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.4k37.3k](/packages/matomo-matomo)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

19562.3M1.3k](/packages/drupal-core)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)

PHPackages © 2026

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