title: How to trigger a script on a Teamcity CMD step failure?
body: <p>I have a TeamCity job that runs a CMD step with an inline bash script.</p>
  
  <p>If a user cancels the job or it fails, I want to send a notification via tcp to another system.</p>
  
  <p>I put a trap handler into the script but the trap is not triggering, suggesting that it's not being passed any of the usual signals.</p>
  
  <pre><code>#!/usr/bin/env bash 
  
  function handle_error() {
      echo "TRAPPED $1"
      exit 1;
  }
  
  trap 'handle_error ERR' ERR
  trap 'handle_error INT' INT
  trap 'handle_error TERM' TERM
  trap 'handle_error QUIT' QUIT
  trap 'handle_error EXIT' EXIT
  
  for i in $(seq 30); do echo $i; sleep 1; done
  </code></pre>
  
  <p>I get the following output when I start the job and then cancel it.</p>
  
  <pre><code>[11:34:36][Step 1/1] Starting: /opt/teamcity_agent_a11/temp/agentTmp/custom_script4059331588446614618
  [11:34:36][Step 1/1] in directory: /opt/teamcity_agent_a11/work/84217cc201b5cd23
  [11:34:36][Step 1/1] 1
  [11:34:37][Step 1/1] 2
  [11:34:38][Step 1/1] 3
  [11:34:39][Step 1/1] 4
  [11:34:40][Step 1/1] 5
  [11:34:43][Step 1/1] Process exited with code 137
  [11:34:43][Step 1/1] Step Command Line interrupted
  [11:34:44]Build was interrupted. Artifacts will not be published for this build
  [11:34:44]Build canceled
  </code></pre>
  
  <p>The TRAPPED message does not appear. Is there a way to make this work?</p>
  
  <p>Alternatively, is there a way to configure TeamCity to trigger another script if this job fails or is cancelled?</p>
  
  <p>--<strong>UPDATE</strong>--</p>
  
  <p>I've now discovered that since <strong>10.0.2</strong>, TeamCity offers control of this behaviour via parameters.</p>
  
  <p>By setting <code>teamcity.force.kill.process.on.cancel.build=false</code>, Teamcity will first send a <strong>SIGTERM</strong> and then follow it with a <strong>SIGKILL</strong> after <strong>60</strong> seconds  (overridable via <code>teamcity.force.kill.process.on.cancel.build.timeout.sec</code>) if still alive</p>
  
  <p>More details in this ticket: <a href="https://youtrack.jetbrains.com/issue/TW-13367#comment=27-1573848" rel="nofollow noreferrer">https://youtrack.jetbrains.com/issue/TW-13367#comment=27-1573848</a></p>