PHPackages                             spatie/laravel-signal-aware-command - 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. [CLI &amp; Console](/categories/cli)
4. /
5. spatie/laravel-signal-aware-command

ActiveLibrary[CLI &amp; Console](/categories/cli)

spatie/laravel-signal-aware-command
===================================

Handle signals in artisan commands

2.1.2(2mo ago)16713.2M—0.7%111MITPHPPHP ^8.2CI passing

Since Apr 4Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/spatie/laravel-signal-aware-command)[ Packagist](https://packagist.org/packages/spatie/laravel-signal-aware-command)[ Docs](https://github.com/spatie/laravel-signal-aware-command)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-laravel-signal-aware-command/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (9)Versions (12)Used By (1)

Handle signals in Artisan commands
==================================

[](#handle-signals-in-artisan-commands)

[![Latest Version on Packagist](https://camo.githubusercontent.com/702b7c15e1060f5329c0492a393aca2a27ac831fd80e5f45a804fd8d4773088e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d7369676e616c2d61776172652d636f6d6d616e642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-signal-aware-command)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b367cf8a0b523385fa57b44c10dcb93fc6d1b2c2fa34cb358659afb3ca45cdfb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d7369676e616c2d61776172652d636f6d6d616e642f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d5465737473)](https://github.com/spatie/laravel-signal-aware-command/actions?query=workflow%3ATests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/52e1c2411d57a3b61657c3922bae62fae2937f55ed65e8924467627a407b4a64/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d7369676e616c2d61776172652d636f6d6d616e642f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d436f64652532305374796c65)](https://github.com/spatie/laravel-signal-aware-command/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/d32c932659b96f0c080669475349528b429108f28c4acf06396690b5f09ae033/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d7369676e616c2d61776172652d636f6d6d616e642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-signal-aware-command)

Using this package you can easily handle signals like `SIGINT`, `SIGTERM` in your Laravel app.

Here's a quick example where the `SIGINT` signal is handled.

```
use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';

    public function handle()
    {
        $this->info('Command started...');

        sleep(100);
    }

    public function onSigint()
    {
        // will be executed when you stop the command

        $this->info('You stopped the command!');
    }
}
```

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/ccb31492ffe0e43f8f80a3e1a5a8439c002ce9dfcd2aa59f11d12d21037dd991/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d7369676e616c2d61776172652d636f6d6d616e642e6a70673f743d31)](https://spatie.be/github-ad-click/package-laravel-signal-aware-command-laravel)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

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

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-signal-aware-command
```

Usage
-----

[](#usage)

In order to make an Artisan command signal aware you need to let it extend `SignalAwareCommand`.

```
use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    // your code
}
```

### Handling signals

[](#handling-signals)

There are three ways to handle signals:

- on the command itself
- via the `Signal` facade
- using the `SignalReceived` event

#### On the command

[](#on-the-command)

To handle signals on the command itself, you need to let your command extend `SignalAwareCommand`. Next, define a method that starts with `on` followed by the name of the signal. Here's an example where the `SIGINT` signal is handled.

```
use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';

    public function handle()
    {
        $this->info('Command started...');

        sleep(100);
    }

    public function onSigint()
    {
        // will be executed when you stop the command

        $this->info('You stopped the command!');
    }
}
```

#### Via the `Signal` facade

[](#via-the-signal-facade)

Using the `Signal` facade you can register signal handling code anywhere in your app.

First, you need to define the signals you want to handle in your command in the `handlesSignals` property.

```
use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';

    protected $handlesSignals = [SIGINT];

    public function handle()
    {
        (new SomeOtherClass())->performSomeWork();

        sleep(100);
    }
}
```

In any class you'd like you can use the `Signal` facade to register code that should be executed when a signal is received.

```
use Illuminate\Console\Command;
use Spatie\SignalAwareCommand\Facades\Signal;

class SomeOtherClass
{
    public function performSomeWork()
    {
        Signal::handle(SIGINT, function(Command $commandThatReceivedSignal) {
            $commandThatReceivedSignal->info('Received the SIGINT signal!');
        })
    }
}
```

You can call `clearHandlers` if you want to remove a handler that was previously registered.

```
use Spatie\SignalAwareCommand\Facades\Signal;

public function performSomeWork()
{
    Signal::handle(SIGNINT, function() {
        // perform cleanup
    });

    $this->doSomeWork();

    // at this point doSomeWork was executed without any problems
    // running a cleanup isn't necessary anymore
    Signal::clearHandlers(SIGINT);
}
```

To clear all handlers for all signals use `Signal::clearHandlers()`.

#### Using the `SignalReceived` event

[](#using-the-signalreceived-event)

Whenever a signal is received, the `Spatie\SignalAwareCommand\Events\SignalReceived` event is fired.

To register which events you want to receive you must define a `handlesSignals` property on your command. Here's an example where we register listening for the `SIGINT` signal.

```
use Spatie\SignalAwareCommand\SignalAwareCommand

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';

    protected $handlesSignals = [SIGINT];

    public function handle()
    {
        (new SomeOtherClass())->performSomeWork();

        sleep(100);
    }
}
```

In any class you'd like you can listen for the `SignalReceived` event.

```
use Spatie\SignalAwareCommand\Events\SignalReceived;
use Spatie\SignalAwareCommand\Signals;

class SomeOtherClass
{
    public function performSomeWork()
    {
        Event::listen(function(SignalReceived $event) {
            $signalNumber = $event->signal;

            $signalName = Signals::getSignalName($signalNumber);

            $event->command->info("Received the {$signalName} signal");
        });
    }
}
```

Learn how this package was built
--------------------------------

[](#learn-how-this-package-was-built)

The foundations of this pacakge were coded up in [this live stream on YouTube](https://www.youtube.com/watch?v=D9hxQoD47jI).

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity63

Solid adoption and visibility

Community25

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% 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 ~178 days

Recently: every ~186 days

Total

11

Last Release

86d ago

Major Versions

v1.x-dev → 2.0.02024-02-05

PHP version history (2 changes)1.0.0PHP ^8.0

2.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (66 commits)")[![stefanzweifel](https://avatars.githubusercontent.com/u/1080923?v=4)](https://github.com/stefanzweifel "stefanzweifel (10 commits)")[![erikn69](https://avatars.githubusercontent.com/u/4933954?v=4)](https://github.com/erikn69 "erikn69 (5 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (4 commits)")[![alexmanase](https://avatars.githubusercontent.com/u/10696975?v=4)](https://github.com/alexmanase "alexmanase (4 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![fredsal](https://avatars.githubusercontent.com/u/79155632?v=4)](https://github.com/fredsal "fredsal (1 commits)")[![binarweb](https://avatars.githubusercontent.com/u/38425768?v=4)](https://github.com/binarweb "binarweb (1 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (1 commits)")[![parallels999](https://avatars.githubusercontent.com/u/109294935?v=4)](https://github.com/parallels999 "parallels999 (1 commits)")[![richardkeep](https://avatars.githubusercontent.com/u/3874381?v=4)](https://github.com/richardkeep "richardkeep (1 commits)")[![thecaliskan](https://avatars.githubusercontent.com/u/13554944?v=4)](https://github.com/thecaliskan "thecaliskan (1 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (1 commits)")[![xHeaven](https://avatars.githubusercontent.com/u/14284867?v=4)](https://github.com/xHeaven "xHeaven (1 commits)")

---

Tags

spatielaravellaravel-signal-aware-command

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-laravel-signal-aware-command/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-signal-aware-command/health.svg)](https://phpackages.com/packages/spatie-laravel-signal-aware-command)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-health

Monitor the health of a Laravel application

86910.0M83](/packages/spatie-laravel-health)[worksome/envy

Automatically keep your .env files in sync.

6871.8M](/packages/worksome-envy)[spatie/laravel-slack-alerts

Send a message to Slack

3212.6M4](/packages/spatie-laravel-slack-alerts)[spatie/laravel-login-link

Quickly login to your local environment

4381.2M1](/packages/spatie-laravel-login-link)[spatie/laravel-onboard

A Laravel package to help track user onboarding steps

808342.9k1](/packages/spatie-laravel-onboard)

PHPackages © 2026

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