I have a Symfony Console
command that iterates over a potentially big collection of items and does a task with each of them. Since the collection can be big, the co
The answers are more complex than they need to be. Sure, you can register POSIX signal handlers, but if the only signals that need to be handled are basic interrupts and the like, you should just define a destructor on the Command.
class YourCommand extends Command
{
// Other code goes here.
__destruct()
{
$this->shouldStop = true;
}
}
A case where you would want to register a POSIX signal is for the SIGCONT
signal, which can handle the resumption of a process that was stopped (SIGSTOP
).
Another case would be where you want every signal to behave differently; for the most part, though, SIGINT
and SIGTERM
and a handful of others would be registered with the same "OMG THE PROCESS HAS BEEN KILLED" operation.
Aside from these examples, registering signal events is unnecessary. This is why destructors exist.
You can even extend Symfony's base Command
class with a __destruct
method, which would automatically provide cleanup for every command; should a particular command require additional operations, just overwrite it.