Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jeffbeard/9941298 to your computer and use it in GitHub Desktop.

Select an option

Save jeffbeard/9941298 to your computer and use it in GitHub Desktop.

Revisions

  1. @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
  2. @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