Polyscope - The agent-first dev environment for Laravel

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
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Shift logo

Shift

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

Shift
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger