Cleanup after Ctrl-C?

I’ve got a build script which starts a long-running task. Because I want to capture and deal with output from the task, it’s actually being spawned by ProcessBuilder “by hand”.

Unfortunately, if you hit Ctrl-C while the task is running, Gradle quits and leaves the spawned process in the background.

There are a few threads about this in the forums, but most are several years old.

Is it currently (Gradle 7.x) possible to react to a Ctrl-C so I can terminate the background process before aborting?

I can write a separate task to kill the process, so I have a sort-of workaround, but impatient users are going to just hit Ctrl-C and then when they try to restart the job, they’ll discover a port it uses is still in use, and it’ll all get confusing.