4

I have a TeamCity job that runs a CMD step with an inline bash script.

If a user cancels the job or it fails, I want to send a notification via tcp to another system.

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.

#!/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

I get the following output when I start the job and then cancel it.

[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

The TRAPPED message does not appear. Is there a way to make this work?

Alternatively, is there a way to configure TeamCity to trigger another script if this job fails or is cancelled?

--UPDATE--

I've now discovered that since 10.0.2, TeamCity offers control of this behaviour via parameters.

By setting teamcity.force.kill.process.on.cancel.build=false, Teamcity will first send a SIGTERM and then follow it with a SIGKILL after 60 seconds (overridable via teamcity.force.kill.process.on.cancel.build.timeout.sec) if still alive

More details in this ticket: https://youtrack.jetbrains.com/issue/TW-13367#comment=27-1573848

Community
  • 1
  • 1
Tom
  • 2,416
  • 3
  • 31
  • 38
  • It is working fine. You just need to add some some statements and try it. For e.g. I just did a `sleep 10` at the end and did a Ctrl+C and SIGINT was trapped properly. – Inian Jan 17 '18 at 11:26
  • Hi Inian - sorry I should have said I tried that already - Updated my question. I dont see the result of the echo "TRAPPED " when I cancel the job. – Tom Jan 17 '18 at 11:36
  • Is there a part of the script you are not showing here? How are the logs generated? How is the script started/stopped – Inian Jan 17 '18 at 11:40
  • It's running under Teamcity as per the title. Starting and stopping via the UI. The traps work fine from a normal terminal execution – Tom Jan 17 '18 at 11:43

2 Answers2

2

I've now discovered that since 10.0.2, Teamcity offers control of this behaviour via parameters.

By setting teamcity.force.kill.process.on.cancel.build=false, Teamcity will first send a SIGTERM and then follow it with a SIGKILL after 60 seconds (overridable via teamcity.force.kill.process.on.cancel.build.timeout.sec) if still alive

More details in this ticket: https://youtrack.jetbrains.com/issue/TW-13367#comment=27-1573848

Tom
  • 2,416
  • 3
  • 31
  • 38
1

According to the Exit Codes With Special Meanings description, exit code 137 means that the process was killed with SIGKILL, which

can not be caught, blocked or ignored

according to the docs (see section 12.1.2. Usage of signals with kill)

cyberskunk
  • 1,722
  • 20
  • 22