# Cleanup after Ctrl-C?

**URL:** <https://discuss.gradle.org/t/cleanup-after-ctrl-c/41203>\
**Category:** Help/Discuss\
**Created:** [October 14, 2021, 4:14pm UTC](https://discuss.gradle.org/t/cleanup-after-ctrl-c/41203 "2021-10-14T16:14:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norm](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/norm/32/1096_2.png) [@Norm](https://discuss.gradle.org/u/Norm)\
**Post date:** [October 14, 2021, 4:14pm UTC](https://discuss.gradle.org/t/cleanup-after-ctrl-c/41203/1 "2021-10-14T16:14:13Z")

</div>

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.
