title: How to gracefully stop job execution when it’s killed on the Rundeck Web UI?
body: <p>This is the same question as <a href="https://stackoverflow.com/q/41729610/735926">How to gracefully stop job execution when one step fails in Rundeck?</a> except that I want to gracefully stop a job when it’s <em>killed</em> from the Web UI.</p>
  
  <p>I’ve a similar job as the other question author: it dumps some SQL tables; manipulate a bit the dumps; upload them somewhere; and cleanup.</p>
  
  <p>The issue comes when the job is terminated or killed before cleanup; it fills the disk with files that never get deleted. Rundeck supports <a href="http://rundeck.org/docs/manual/jobs.html#error-handlers" rel="nofollow noreferrer">error handlers</a> for this but the docs say it’s for errors, not manual kills from the UI.</p>
  
  <p>I’m able to cleanup on exit (success or error) by using the following in my script:</p>
  
  <pre><code>#!/bin/bash -ex
  finish() {
    echo "remove dir $OUTPUT..."
    rm -Rf "$OUTPUT"
  }
  trap finish EXIT
  # ... rest of the script ...
  </code></pre>
  
  <p>However this step doesn’t execute when the job is killed from the UI. I dug in Rundeck’s code source and it appears to simply use <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#interrupt--" rel="nofollow noreferrer"><code>Thread#interrupt</code></a>, which I probably can’t catch.</p>
  
  <p>Is there any solution to this issue? Right now I must <code>ssh</code> into the node and clean things up by myself when I kill the job.</p>