PHPackages                             seld/signal-handler - 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. seld/signal-handler

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

seld/signal-handler
===================

Simple unix signal handler that silently fails where signals are not supported for easy cross-platform development

2.0.2(2y ago)18271.3M—4.9%317MITPHPPHP &gt;=7.2.0

Since Sep 15Pushed 1y ago5 watchersCompare

[ Source](https://github.com/Seldaek/signal-handler)[ Packagist](https://packagist.org/packages/seld/signal-handler)[ RSS](/packages/seld-signal-handler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (6)Versions (9)Used By (17)

Signal Handler
==============

[](#signal-handler)

A simple cross-platform1 signal handler.

1 Windows support is only possible on PHP 7.4+ and only supports catching `SIGINT`/`SIGBREAK` (`ctrl+c` / `ctrl+break` respectively). On older PHP versions it will simply silently fail to work on Windows so you can use it but signals will just interrupt PHP as if the library was not in use.

[![Continuous Integration](https://github.com/Seldaek/signal-handler/workflows/Continuous%20Integration/badge.svg?branch=main)](https://github.com/Seldaek/signal-handler/actions)

Usage
-----

[](#usage)

> **Note**: To maximize cross-platform support all signals are available as constants on the SignalHandler class and it is recommended to use those instead of the PHP constants as the constants are not available on all platforms.

### Default usage, listen to SIGTERM and SIGINT (i.e. Ctrl+C / ^C interrupts)

[](#default-usage-listen-to-sigterm-and-sigint-ie-ctrlc--c-interrupts)

```
use Seld\Signal\SignalHandler;

$signal = SignalHandler::create();

while (true) {
    // do some work here ...

    // exit gracefully at the end of an iteration if the process interruption was called for
    if ($signal->isTriggered()) {
        $signal->exitWithLastSignal();
    }
}
```

### Nesting/stacking and unregistering signal handlers

[](#nestingstacking-and-unregistering-signal-handlers)

If you create multiple `SignalHandler` instances they will be kept on a stack and only the top-most / last created one will be triggered when a signal comes in.

When you unregister the top one the previous one becomes active again, etc.

For this to work well however you need to make sure you properly unregister the signal handler when you are done with it. On PHP 8.0+ this is done automatically whenever the signal handler is garbage collected as the stack uses [WeakReference](https://www.php.net/manual/en/class.weakreference.php)instances to keep track of the handlers. On PHP 7 however you need to call `$signal->unregister();`explicitly to remove it from the global handler stack.

Typically it would look something like this if you need PHP 7 support:

```
use Seld\Signal\SignalHandler;

$signal = SignalHandler::create();

try {
    // do things
} finally {
    $signal->unregister();
}
```

### Listen to custom signals and reset the handler to handle the same signal multiple times

[](#listen-to-custom-signals-and-reset-the-handler-to-handle-the-same-signal-multiple-times)

```
use Seld\Signal\SignalHandler;

// using strings for the constant names makes sure the code will run on Windows and
// OSX even if the signal is missing on those platforms
$signal = SignalHandler::create([SignalHandler::SIGHUP, SignalHandler::SIGUSR1]);

while (true) {
    // do some work here ...

    // reload the config when the signal was triggered
    if ($signal->isTriggered()) {
        $this->reloadConfiguration();

        // reset the handler so next time you check isTriggered() it
        // will be false, until SIGHUP/SIGUSR1 is signaled again
        $signal->reset();
    }
}
```

### Passing in a [PSR-3 Logger](https://packagist.org/providers/psr/log-implementation) will make it log `->info('Received '.$signalName)`

[](#passing-in-a-psr-3-logger-will-make-it-log--inforeceived-signalname)

```
use Seld\Signal\SignalHandler;

$signal = SignalHandler::create(null, new PSR3Logger());
```

### Passing in a callback you can react to the signal as well

[](#passing-in-a-callback-you-can-react-to-the-signal-as-well)

```
use Seld\Signal\SignalHandler;

$signal = SignalHandler::create([SignalHandler::SIGINT], function (string $signalName, SignalHandler $self) {
    echo 'Received ' . $signalName . PHP_EOL;

    // you can optionally receive the SignalHandler instance as second arg to do things on it like
    // resetting its state or exiting to handle the signal
    $self->reset();
    $self->exitWithLastSignal();
});
```

> **Warning**: As described above on PHP 8.0+ this library uses weak references to keep track of the handlers, so if you do not store the result of `::create()` into a variable that you keep around as long as you need the handler, it will not work and your callback function will never be called.

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

[](#installation)

For a quick install with Composer use:

```
$ composer require seld/signal-handler

```

Requirements
------------

[](#requirements)

- PHP 7.2+ (or PHP 5.4+ for v1 which is EOL now)

Submitting bugs and feature requests
------------------------------------

[](#submitting-bugs-and-feature-requests)

Bugs and feature request are tracked on [GitHub](https://github.com/Seldaek/signal-handler/issues)

Author
------

[](#author)

Jordi Boggiano -  -

License
-------

[](#license)

signal-handler is licensed under the MIT License - see the LICENSE file for details

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity66

Solid adoption and visibility

Community27

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 87.5% 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 ~485 days

Recently: every ~374 days

Total

7

Last Release

989d ago

Major Versions

1.3.0 → 2.0.02022-07-20

PHP version history (3 changes)1.0.0PHP &gt;=5.4.0

1.3.0PHP &gt;=7.1.0

2.0.0PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b69782e1d7f426ef78f61d159c466a536372eff931f13981ac578fa3f4649352?d=identicon)[Seldaek](/maintainers/Seldaek)

---

Top Contributors

[![Seldaek](https://avatars.githubusercontent.com/u/183678?v=4)](https://github.com/Seldaek "Seldaek (35 commits)")[![fbouchery](https://avatars.githubusercontent.com/u/148793717?v=4)](https://github.com/fbouchery "fbouchery (2 commits)")[![pintomau](https://avatars.githubusercontent.com/u/1271442?v=4)](https://github.com/pintomau "pintomau (1 commits)")[![sergeyklay](https://avatars.githubusercontent.com/u/1256298?v=4)](https://github.com/sergeyklay "sergeyklay (1 commits)")[![vtsykun](https://avatars.githubusercontent.com/u/21358010?v=4)](https://github.com/vtsykun "vtsykun (1 commits)")

---

Tags

signalunixposixsigintsigterm

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/seld-signal-handler/health.svg)

```
[![Health](https://phpackages.com/badges/seld-signal-handler/health.svg)](https://phpackages.com/packages/seld-signal-handler)
```

###  Alternatives

[cocur/background-process

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

2971.9M12](/packages/cocur-background-process)[arara/process

Provides a better API to work with processes on Unix-like systems

16861.7k2](/packages/arara-process)[misterion/ko-process

Simple pcntl fork wrapper and process manager

177337.7k6](/packages/misterion-ko-process)[jbzoo/event

Library for event-based development

29760.0k5](/packages/jbzoo-event)[braincrafted/background-process

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

2976.1k1](/packages/braincrafted-background-process)[aura/signal

A SignalSlots/EventHandler implementation; with it, we can invoke handlers ('slots' or 'hooks') whenever an object sends a signal ('notification' or 'event') to the signal manager.

3361.6k4](/packages/aura-signal)

PHPackages © 2026

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