#development #laravel #php

As you may know, you can use the shortcut Ctrl + C in the console to terminate a process running in the foreground. It interrupts the current foreground process by sending the SIGINT signal to the process. Basically, this is just a request to stop the current process. What is very good about it is that you can also intercept and process this signal using PHP. Let's take a look how you can integrate it in your own console command:

1public function handle()
2{
3    // Do some stuff in your command
4    // Catch the cancellation (Ctrl+C)
5    pcntl_async_signals(true);
6    pcntl_signal(SIGINT, function () use ($master) {
7        $this->line('You pressed Ctrl + C');
8    });
9}