Forked from gerner/kill-processes-orphaned-by-jenkins.sh
Last active
August 29, 2015 13:57
-
-
Save mshuler/9374881 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Kill processes orphaned by Jenkins | |
# Work around Java's use of SIGTERM rather than SIGKILL and | |
# Jenkins's lack of any workaroud in the box. | |
# here is the relevant bug: | |
# https://issues.jenkins-ci.org/browse/JENKINS-17116 | |
# Suggested usage: | |
# | |
# $ crontab -l | |
# */5 0 0 0 0 /path/to/kill-processes-orphaned-by-jenkins.sh <jenkins_username> <jenkins auth> false 2>&1 | logger | |
# Tested on Linux Ubuntu 11.04, 12.04 | |
# NOTE: this must run as the jenkins user or root to see the environment variables (needed to list build urls) | |
#JENKINS_USERNAME=jenkins | |
#USER_AUTH=<username>:<password> | |
#DRYRUN=true | |
JENKINS_USERNAME=$1 | |
USER_AUTH=$2 | |
DRYRUN=$3 | |
if [ "$DRYRUN" != "false" ]; then DRYRUN=true; fi | |
for url in $(ps e -U $JENKINS_USERNAME | grep -o "[B]UILD_URL=[^ ]*" | awk -F'=' '{print $2}' | sort -u) | |
do | |
curl -s -u "$USER_AUTH" "$url"'/api/json?tree=building' | grep -q true | |
building=$? | |
false=1 | |
if [ $building -eq $false ] | |
then | |
for pid in $(ps e -U $JENKINS_USERNAME | grep "$url" | awk '{print $1}') | |
do | |
echo 'Finished job: '"$url" | |
ps -f -p $pid | |
echo | |
if ! $DRYRUN; then | |
echo "Killing process $pid orphaned by Jenkins:" | |
kill -9 $pid | |
else | |
echo "dry run, don't kill $pid" | |
fi | |
done | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment