Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mshuler/9374881 to your computer and use it in GitHub Desktop.

Select an option

Save mshuler/9374881 to your computer and use it in GitHub Desktop.

Revisions

  1. mshuler revised this gist Mar 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ 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)
    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=$?
  2. @gerner gerner revised this gist Jan 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    # Suggested usage:
    #
    # $ crontab -l
    # */5 0 0 0 0 /path/to/kill-processes-orphaned-by-jenkins.sh 2>&1 | logger
    # */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)
  3. @gerner gerner revised this gist Jan 27, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@
    # */5 0 0 0 0 /path/to/kill-processes-orphaned-by-jenkins.sh 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>
  4. @gerner gerner revised this gist Jan 27, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@
    JENKINS_USERNAME=$1
    USER_AUTH=$2
    DRYRUN=$3
    if [ -z "$DRYRUN" ]; then DRYRUN=true; fi
    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
    @@ -38,6 +38,8 @@ do
    if ! $DRYRUN; then
    echo "Killing process $pid orphaned by Jenkins:"
    kill -9 $pid
    else
    echo "dry run, don't kill $pid"
    fi
    done
    fi
  5. @gerner gerner revised this gist Jan 27, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,6 @@ do
    false=1
    if [ $building -eq $false ]
    then
    for pid in $(ps | grep "$url" | grep -v ' grep' | awk '{ print $2 }')
    for pid in $(ps e -U $JENKINS_USERNAME | grep "$url" | awk '{print $1}')
    do
    echo 'Finished job: '"$url"
  6. @gerner gerner revised this gist Jan 27, 2014. 1 changed file with 20 additions and 10 deletions.
    30 changes: 20 additions & 10 deletions kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -4,32 +4,42 @@

    # 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 2>&1 | logger

    # Tested on Mac OS X (BSD). Probably needs `ps` arguments
    # ported for Linux (GNU).
    # Tested on Linux Ubuntu 11.04, 12.04

    for url in $(ps -wwEf | grep BUILD_URL | grep -v ' grep' | tr ' ' '\n' | grep BUILD_URL | sort | uniq | awk -F'=' '{ print $2 }')
    #JENKINS_USERNAME=jenkins
    #USER_AUTH=<username>:<password>
    #DRYRUN=true

    JENKINS_USERNAME=$1
    USER_AUTH=$2
    DRYRUN=$3
    if [ -z "$DRYRUN" ]; then DRYRUN=true; fi

    for url in $(ps e -U $JENKINS_USERNAME | grep -o "BUILD_URL=[^ ]*" | awk -F'=' '{print $2}' | sort -u)
    do
    # Get your API token from
    # http://<jenkins-host>/user/<user>/configure > Show API
    # Token...
    curl -s -u '<user>:<token>' "$url"'/api/json?tree=building' | grep -q true
    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 -wwEf | grep "$url" | grep -v ' grep' | awk '{ print $2 }')
    for pid in $(ps | grep "$url" | grep -v ' grep' | awk '{ print $2 }')
    for pid in $(ps e -U $JENKINS_USERNAME | grep "$url" | awk '{print $1}')
    do
    echo 'Finished job: '"$url"
    echo 'Killing process orphaned by Jenkins:'
    ps -f -p $pid
    echo
    kill -9 $pid
    if ! $DRYRUN; then
    echo "Killing process $pid orphaned by Jenkins:"
    kill -9 $pid
    fi
    done
    fi
    done
  7. @matthewlmcclure matthewlmcclure revised this gist Jan 16, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,17 @@
    #! /bin/bash

    # Tested on Mac OS X (BSD). Probably needs `ps` arguments ported for Linux (GNU).
    # 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.

    # Suggested usage:
    #
    # $ crontab -l
    # */5 0 0 0 0 /path/to/kill-processes-orphaned-by-jenkins.sh 2>&1 | logger

    # Tested on Mac OS X (BSD). Probably needs `ps` arguments
    # ported for Linux (GNU).

    for url in $(ps -wwEf | grep BUILD_URL | grep -v ' grep' | tr ' ' '\n' | grep BUILD_URL | sort | uniq | awk -F'=' '{ print $2 }')
    do
  8. @matthewlmcclure matthewlmcclure created this gist Jan 16, 2014.
    24 changes: 24 additions & 0 deletions kill-processes-orphaned-by-jenkins.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #! /bin/bash

    # Tested on Mac OS X (BSD). Probably needs `ps` arguments ported for Linux (GNU).

    for url in $(ps -wwEf | grep BUILD_URL | grep -v ' grep' | tr ' ' '\n' | grep BUILD_URL | sort | uniq | awk -F'=' '{ print $2 }')
    do
    # Get your API token from
    # http://<jenkins-host>/user/<user>/configure > Show API
    # Token...
    curl -s -u '<user>:<token>' "$url"'/api/json?tree=building' | grep -q true
    building=$?
    false=1
    if [ $building -eq $false ]
    then
    for pid in $(ps -wwEf | grep "$url" | grep -v ' grep' | awk '{ print $2 }')
    do
    echo 'Finished job: '"$url"
    echo 'Killing process orphaned by Jenkins:'
    ps -f -p $pid
    echo
    kill -9 $pid
    done
    fi
    done