Возможно ли прослушать CTRL+C при запуске скрипта groovy из командной строки?
У меня есть скрипт, который создает несколько файлов. При прерывании я хочу удалить их с диска, а затем завершить.
Возможно ли это?
UPDATE 1: Заимствовано из ответа @tim_yates:
def withInteruptionListener = { Closure cloj, Closure onInterrupt ->
def thread = { onInterrupt?.call() } as Thread
Runtime.runtime.addShutdownHook (thread)
cloj();
Runtime.runtime.removeShutdownHook (thread)
}
withInteruptionListener ({
println "Do this"
sleep(3000)
throw new java.lang.RuntimeException("Just to see that this is also taken care of")
}, {
println "Interupted! Clean up!"
})