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