Laravel Signal Aware Command
Laravel Signal Aware Command stats
- Downloads
- 3.2M
- Stars
- 106
- Open Issues
- 0
- Forks
- 9
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
SignalAwareCommandto 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
SignalFacade: Register signal handling code anywhere in your app using a facade. - Via Events: Listen for the
SignalReceivedevent 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
-
In the Command Class:
public function onSigint(){$this->info('SIGINT received. Command stopping.');} -
Using the
SignalFacade:use Spatie\SignalAwareCommand\Facades\Signal;Signal::handle(SIGINT, function ($command) {$command->info('SIGINT received!');}); -
Using the
SignalReceivedEvent:use Spatie\SignalAwareCommand\Events\SignalReceived;Event::listen(function (SignalReceived $event) {$event->command->info("Received signal: {$event->signal}");});
Additional Resources
- Live Stream Tutorial: Watch how this package was built.
- Contributing Guide: Guidelines for contributing to the package.
- Changelog: Detailed list of changes across different versions.
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.