Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gerner/8654625 to your computer and use it in GitHub Desktop.
Save gerner/8654625 to your computer and use it in GitHub Desktop.
#! /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 "BUILD_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