1

This question is very similar to Is there a standard command that always exits with a failure?

I'm writing some code which I need to test that it handles subprocesses gracefully when the child process exits due to a signal (say SIGTERM or SIGINT).

Is there something concise that I can call like true or false to achieve this with signals?

flakes
  • 113
  • 5

1 Answers1

3

Ok :-)

The script you want looks like this:

#! /bin/sh

kill $$

This relies on the fact that kill is a builtin command, and not the /bin/kill program.

q.undertow
  • 518
  • 2
  • 10
  • 1
    Thanks so much! For future Python folks doing something similar: `Popen(["bash", "-c", "kill -SIGTERM $$"])` for my unit test! – flakes Oct 10 '20 at 01:27
  • 2
    What difference would it make if `kill` was an external program? –  Oct 10 '20 at 02:42
  • Could you say something more about that last statement of yours about `kill` needing to be built-in? – Kusalananda Oct 10 '20 at 07:11
  • 1
    It might be more useful for testing if it took two args: (a) A time to sleep -- children that instantly die may not be a fair test. (b) A signal to send itself -- your control code might want to report the child's reason for failure. – Paul_Pedant Oct 10 '20 at 11:15
  • @user414777 you're right, it doesn't matter. It would work with /bin/kill as well. – q.undertow Oct 12 '20 at 02:05
  • 1
    @Paul_Pedant okay, make the last line `sleep ${1:-1} ; kill -n ${2:-15} $$` – q.undertow Oct 12 '20 at 02:08