Two Laravel devs that won't disappear on you. Finally! Hire Joel and Aaron from No Compromises.

Laravel Signal Aware Command

spatie/laravel-signal-aware-command image

Laravel Signal Aware Command stats

Downloads
3.2M
Stars
106
Open Issues
0
Forks
9

View on GitHub →

Handle signals in artisan commands

Laravel Signal-Aware Command by Spatie

The spatie/laravel-signal-aware-command package enables Laravel applications to handle system signals within Artisan commands gracefully. This package is especially useful for managing long-running commands by allowing them to respond to system signals like SIGINT and SIGTERM.

Key Features

  • Signal Handling in Artisan Commands: Extend the base SignalAwareCommand to make any Artisan command responsive to operating system signals.
  • Multiple Ways to Handle Signals:
    • Directly in Commands: Define methods in the command class itself to handle specific signals.
    • Using the Signal Facade: Register signal handling code anywhere in your app using a facade.
    • Via Events: Listen for the SignalReceived event and handle signals dynamically.
  • Easy Installation and Usage: Simply install via Composer and extend your commands from SignalAwareCommand.

Installation

Install the package through Composer:

composer require spatie/laravel-signal-aware-command

Usage

To make an Artisan command signal-aware, extend SignalAwareCommand:

use Spatie\SignalAwareCommand\SignalAwareCommand;
 
class YourCommand extends SignalAwareCommand
{
// Command implementation
}

Handling Signals

  1. In the Command Class:

    public function onSigint()
    {
    $this->info('SIGINT received. Command stopping.');
    }
  2. Using the Signal Facade:

    use Spatie\SignalAwareCommand\Facades\Signal;
     
    Signal::handle(SIGINT, function ($command) {
    $command->info('SIGINT received!');
    });
  3. Using the SignalReceived Event:

    use Spatie\SignalAwareCommand\Events\SignalReceived;
     
    Event::listen(function (SignalReceived $event) {
    $event->command->info("Received signal: {$event->signal}");
    });

Additional Resources

This package is a robust solution for handling system signals in Laravel applications, enhancing the control over command-line tasks and their interaction with the system environment.

spatie photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Laravel Signal Aware Command Related Articles

Handling Signals in Laravel Console Commands image

Handling Signals in Laravel Console Commands

Read article
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm