2

I'm using make to run a development webserver (among other things). After the server quits, I want to perform some cleanup task. The server is exited after I press Ctrl+C, but this causes make to stop any further execution of commands. As a simple example Makefile I provide the following:

default:
    sleep 10
    echo hello

When I call make and then press Ctrl+C before the time is up, I don't see "hello". What I've tried (as per this answer):

  • Running make with -i, -k, or both
  • Changing the second entry to -sleep 10 or sleep 10 | true

1 Answer 1

2

I found the answer over on StackOverflow: Split the command in two, and trap SIGINT in the "outer" one:

external-target:
    bash -c "trap 'trap - SIGINT SIGTERM ERR; echo hello; exit 1' SIGINT SIGTERM ERR; $(MAKE) internal-target"

internal-target:
    sleep 10
3
  • 1
    This doesn't seem to work with GNU Make 4.1. I still get Makefile:5: recipe for target 'internal-target' failed and make: *** [internal-target] Interrupt. Jan 27, 2021 at 17:06
  • @MikkoRantalainen I don't have a system with Make 4.1 available; I confirmed that this still works with GNU Make 3.81. So it doesn't print the "hello" anymore after you ^C?
    – L3viathan
    Jan 28, 2021 at 12:05
  • 1
    It does say "hello" but if you add an another command to external-target after the line starting with bash (e.g. echo "Complete.") you'll find that make still aborts with make[1]: *** [internal-target] Interrupt. However, I did find a way to make this work, see stackoverflow.com/a/65935205/334451 for details. Jan 29, 2021 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .