spatie/laravel-signal-aware-command
Add signal awareness to Laravel Artisan commands. Gracefully handle SIGINT/SIGTERM (Ctrl+C, container stop, deploy) to stop work cleanly, run shutdown logic, and avoid half-finished jobs—ideal for long-running CLI tasks and workers.
composer require spatie/laravel-signal-aware-commandSpatie\SignalAwareCommand\BaseSignalAwareCommand in your artisan command instead of Commandhandle() method using $this->onSignal()protected function handle()
{
$this->onSignal(SIGTERM, fn () => $this->info('Shutting down gracefully...'));
while ($this->shouldRun) {
// Process jobs...
usleep(100_000);
}
}
$this->onSignal(SIGTERM, fn () => $this->saveCheckpointAndExit());
$this->shouldRun = false) inside the signal callback to break the main loop.$this->protectFromSignal() to prevent interruption during atomic operations (e.g., file writes).$command->simulateSignal(SIGTERM) in tests to validate shutdown behavior without spawning actual processes.sleep, usleep, or I/O). Long CPU-bound loops may ignore signals — use pcntl_signal_dispatch() manually if needed.prepend option in onSignal().pcntl and posix extensions — ensure php artisan -V runs without warnings.How can I help you explore Laravel packages today?