bin/murder @ ea0425ea8e9f

Add "weechat/python/autoload/local.py" to .hgignore.
author Steve Losh <steve@stevelosh.com>
date Tue, 11 Feb 2014 11:23:16 -0500
parents 761550d1394f
children 758d680aa791
#!/bin/bash
# https://gist.github.com/anonymous/32b1e619bc9e7fbe0eaa

# Signal numbers to use. Configurable, as long as the number of elements in this
# array matches the number of elements in $delays.
signals=(15 2 1 9)

# How much time processes should be given to finish their work before we try the
# next signal. Configurable, as long as the number of elements in this array
# matches the number of elements in $signals.
delays=(4 6 8 0)

# Takes a list of PIDs and ends the processes through increasingly rude means.
function mykill {
    local ps='ps'
    ps="$ps o pid="

    # Figure out the names of the signals we will try.
    local names=($(kill -l ${signals[@]}))

    local retval=0
    if [[ $# -eq 0 ]]; then
        printf 'mykill: Too few arguments: %d\nmykill: Usage: mykill <PIDs...>\n' $#
        retval=1
    else
        for pid in $@; do
            local index=0
            # Try the next signal if the process is alive AND there are more
            # signals to attempt AND the previous kill reported no errors.
            while $ps $pid > /dev/null && [[ $index -lt ${#signals} ]] && [[ $retval -eq 0 ]]; do
                output=$(kill -s ${signals[$index]} $pid 2>&1)
                retval=$?
                local killed=1
                # Give the process some time to finish.
                sleep ${delays[$index]}
                index=$(($index + 1))
            done

            if [[ -z $killed ]]; then
                printf 'mykill: Process with PID %d does not exist.\n' $pid

            elif ! ${ps} ${pid} > /dev/null; then
                index=$(($index - 1))
                printf 'mykill: Killed process with PID %d with signal %s (%d).\n' $pid ${names[$index]} ${signals[$index]}

            elif [[ ${retval} -ne 0 ]]; then
                printf 'mykill: kill failed: %s\n' $(echo ${output} | cut -d ' ' -f 5-)

            elif [[ ${index} -eq ${#signals} ]]; then
                printf 'mykill: Failed to kill process with PID %d with signals %s\n' $pid "$(echo ${names[@]})"
                retval=-1
            fi
        done
    fi
    return ${retval}
}

mykill $@