@@ -178,11 +178,31 @@ 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
+
201
+ .. versionadded :: 6.3
202
+
203
+ The ``setExitCode() ``, ``getExitCode() `` and ``abortExit() `` methods were introduced
204
+ in Symfony 6.3.
205
+
186
206
.. tip ::
187
207
188
208
All the available signals (``SIGINT ``, ``SIGQUIT ``, etc.) are defined as
@@ -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 since 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