@@ -178,16 +178,36 @@ Listeners receive a
178
178
// gets the signal number
179
179
$signal = $event->getHandlingSignal();
180
180
181
+ // sets the exit code
182
+ $event->setExitCode(0);
183
+
181
184
if (\SIGINT === $signal) {
182
185
echo "bye bye!";
183
186
}
184
187
});
185
188
189
+ It is also possible to abort the exit if you want the command to continue its
190
+ execution even after the event has been dispatched, thanks to the
191
+ :method: `Symfony\\ Component\\ Console\\ Event\\ ConsoleSignalEvent::abortExit `
192
+ method::
193
+
194
+ use Symfony\Component\Console\ConsoleEvents;
195
+ use Symfony\Component\Console\Event\ConsoleSignalEvent;
196
+
197
+ $dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) {
198
+ $event->abortExit();
199
+ });
200
+
186
201
.. tip ::
187
202
188
203
All the available signals (``SIGINT ``, ``SIGQUIT ``, etc.) are defined as
189
204
`constants of the PCNTL PHP extension `_.
190
205
206
+ .. versionadded :: 6.3
207
+
208
+ The ``setExitCode() ``, ``getExitCode() `` and ``abortExit() `` methods were introduced
209
+ in Symfony 6.3.
210
+
191
211
If you use the Console component inside a Symfony application, commands can
192
212
handle signals themselves. To do so, implement the
193
213
:class: `Symfony\\ Component\\ Console\\ Command\\ SignalableCommandInterface ` and subscribe to one or more signals::
@@ -208,13 +228,17 @@ handle signals themselves. To do so, implement the
208
228
return [\SIGINT, \SIGTERM];
209
229
}
210
230
211
- public function handleSignal(int $signal): void
231
+ public function handleSignal(int $signal): int|false
212
232
{
213
233
if (\SIGINT === $signal) {
214
234
// ...
215
235
}
216
236
217
237
// ...
238
+
239
+ // return an integer to set the exit code, or
240
+ // false to continue normal execution
241
+ return 0;
218
242
}
219
243
}
220
244
@@ -228,6 +252,10 @@ handle all signals e.g. to do some tasks before terminating the command.
228
252
``SIGUSR2 ``) would terminate the script by calling ``exit(0) ``. Starting
229
253
from Symfony 6.3, no more signal is automatically calling ``exit(0) ``.
230
254
255
+ .. deprecated :: 6.3
256
+
257
+ Not returning a value in ``handleSignal() `` is deprecated in Symfony 6.3.
258
+
231
259
.. _`reserved exit codes` : https://www.tldp.org/LDP/abs/html/exitcodes.html
232
260
.. _`Signals` : https://en.wikipedia.org/wiki/Signal_(IPC)
233
261
.. _`constants of the PCNTL PHP extension` : https://www.php.net/manual/en/pcntl.constants.php
0 commit comments