Skip to content

Commit 8e16338

Browse files
authored
Merge pull request #231 from byexamples/Issue-230-More-Signals-to-Stop
Issue 230 more signals to stop
2 parents ec6a3c8 + 093f2fa commit 8e16338

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

byexample/modules/shell.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def extend_option_parser(self, parser):
103103
)
104104
parser.add_argument(
105105
"+stop-signal",
106-
choices=['suspend', 'eof', 'interrupt'],
106+
choices=['suspend', 'eof', 'interrupt', 'quit'],
107107
default='suspend',
108108
help=
109109
"signal to send when stop-on-timeout/stop-on-silence is used (suspend ^Z by default)."
@@ -201,7 +201,19 @@ def _run_impl(self, example, options):
201201
self._sendcontrol('d')
202202
elif stop_signal == "interrupt":
203203
self._sendcontrol('c')
204+
elif stop_signal == "quit":
205+
self._sendcontrol('\\')
204206
else:
207+
# Note: we could send any signal but experiment shows that
208+
# some of the signals are catched by the shell. SIGKILL for
209+
# example kills the interpreter.
210+
# Other control sequences can break the flow of the terminal
211+
# like ^Q and ^S which start and stop the flow. These are
212+
# too risky to use.
213+
# For these reasons we don't support arbitrary signals
214+
# If the user want to send an arbitrary signal, it should
215+
# use ^Z (suspend) and then run
216+
# kill -NN and send the signal that the user wants.
205217
raise ValueError(
206218
"Unexpected stop-signal '%s'" % stop_signal
207219
)

docs/languages/shell.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ $ (sleep 0.4 ; echo "a slow line" >> w/msg.log) &
269269
$ fg %1 # byexample: +stop-on-silence=0.5
270270
tail -f w/msg.log
271271
a slow line
272+
273+
$ jobs
274+
[1]<...>Stopped<...>
272275
```
273276
274277
> **Note:** ``+stop-on-silence`` requires the job control and monitoring to be
@@ -279,6 +282,10 @@ a slow line
279282
> timeout. But in ``8.0.0`` this option was fixed and the old behaviour can
280283
> be achieved using ``+stop-on-timeout``.
281284
285+
<!--
286+
$ kill -9 $(jobs -p) && wait # byexample: -skip +pass
287+
-->
288+
282289
### Stopping on timeout
283290
284291
``+stop-on-silence`` will stop a process if this one times out, which
@@ -326,6 +333,39 @@ $ cat - # byexample: +stop-on-silence +stop-signal=eof
326333
327334
> *New* in ``byexample 10.1.0``.
328335
336+
Signals like `SIGTERM` and `SIGKILL` cannot be used from
337+
`+stop-signal`; the closest thing is the `quit` signal (`SIGQUIT` or `^\`):
338+
339+
```shell
340+
$ cat - # byexample: +stop-on-silence +stop-signal=quit
341+
```
342+
343+
> *New* in ``byexample 10.5.2``.
344+
345+
### Arbitrary signals on stop
346+
347+
If you want to *kill* a process with `SIGKILL` or `SIGTERM`
348+
you will have to stop the process first with a `suspend` signal (the
349+
default for `+stop-signal`), capture the process' id or job's id and
350+
kill it calling `kill`:
351+
352+
```shell
353+
$ cat - # byexample: +stop-on-silence
354+
355+
$ # The following sends a SIGTERM. Replace the -15 by -9 to send
356+
$ # a SIGKILL. See signal(7).
357+
$ kill -15 %%
358+
<...>
359+
```
360+
361+
In fact, you can use this trick to send any signal like `SIGUSR1`.
362+
In some cases you will have to bring the process to foreground (with
363+
`fg` to give it a chance to receive the signal).
364+
365+
```shell
366+
$ fg # byexample: +pass
367+
```
368+
329369
## Using other shells
330370
331371
``byexample`` supports ``bash``, ``dash`` and ``ksh`` and the shell
@@ -378,7 +418,7 @@ Or you could run ``bash`` without the constraint to be POSIX-conformant
378418
by default (but see `set +o posix` as mentioned above before trying to do this):
379419
380420
```shell
381-
$ byexample -l shell -x-shebang 'shell:%e bash --norc --noprofile' test/ds/shell-example
421+
$ byexample -l shell -x-shebang 'shell:%e bash --norc --noprofile --noediting' test/ds/shell-example
382422
<...>
383423
[PASS] Pass: 14 Fail: 0 Skip: 0
384424
```
@@ -399,7 +439,7 @@ shell's specific options
399439
+stop-on-silence [secs]
400440
stop the process if no output is read in the last
401441
<secs> seconds (0.2 secs by default).
402-
+stop-signal {suspend,eof,interrupt}
442+
+stop-signal {suspend,eof,interrupt,quit}
403443
signal to send when stop-on-timeout/stop-on-silence is
404444
used (suspend ^Z by default).
405445
+shell {bash,dash,ksh,sh}

0 commit comments

Comments
 (0)