Is there any way to run code after a timeout is reached but before the interrupt signal is sent?

I’m using the timeout step in my pipeline around a bat step.

try {
  timeout(time: 2700, unit: 'SECONDS'){
    output = bat ( returnStatus: true, script: commandToRun )
  }
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e){
  //Command timed out!
}

I’m wondering if there’s any way to run some code if the timeout is reached, before the command in the bat step is terminated by Jenkins? I’ve searched around, and even asked AI, but I can’t find much information on the topic. Is it maybe possible to catch an exception higher up the chain, or some other less well known method?

Sadly, catching exceptions won’t do good here; even if you were to do it backwards, timeout { try {...} catch {...} }, by the time you land in catch{} the kill signal has already been sent.

There been a discussion on this in comments of https://issues.jenkins.io/browse/JENKINS-53315 several years ago. The author of it proposed several hackish workarounds in jenkins-scripts/pipeline-timeout-prekill at master · akomakom/jenkins-scripts · GitHub that might or might not be of some use to you.

May I ask, what exactly are you trying to achieve, that you need the process alive by the timeout?

Just an idea you could try to split this in 2 different threads with a parallel step
The first does the actual workload. Once it finished it sets a property,
the other thread will just sleep for a few seconds and then checks if the time is over or the other thread has finished and set the property. If the time is over it can do whatever it needs to cleanup things while the other is still running and then fails. If the cleanup makes the other thread fail then good otherwise you will need failFast for the parallel step probably.
It’s a bit hacky and when you use the sleep step in that waiting thread it will spam your logs.