github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/linux_backend/skeleton/stop.sh (about)

     1  #!/bin/bash
     2  
     3  [ -n "$DEBUG" ] && set -o xtrace
     4  set -o nounset
     5  set -o errexit
     6  shopt -s nullglob
     7  
     8  cd $(dirname $0)
     9  
    10  if [ ! -f ./run/wshd.pid ]
    11  then
    12    echo "wshd is not running..."
    13    exit 1
    14  fi
    15  
    16  WAIT=10
    17  
    18  source etc/config
    19  
    20  function usage() {
    21    echo "Usage $0 [OPTION]..." >&2
    22    echo "  -w N seconds to wait before sending SIGKILL;" >&2
    23    echo "       N=0 skips SIGTERM and sends SIGKILL immediately" >&2
    24    exit 1
    25  }
    26  
    27  while getopts ":w:h" opt
    28  do
    29    case $opt in
    30      "w")
    31        WAIT=$OPTARG
    32        ;;
    33      "h")
    34        usage
    35        ;;
    36      "?")
    37        # Ignore invalid options
    38        ;;
    39    esac
    40  done
    41  
    42  function ms() {
    43    echo $(($(date +%s%N)/1000000))
    44  }
    45  
    46  ms_start=$(ms)
    47  ms_end=$(($ms_start + ($WAIT * 1000)))
    48  
    49  pid=$(cat ./run/wshd.pid)
    50  cgroup_path_segment=$(cat /proc/self/cgroup | grep cpu: | cut -d ':' -f 3)
    51  path=${GARDEN_CGROUP_PATH}/cpu${cgroup_path_segment}/instance-$id
    52  tasks=$path/cgroup.procs
    53  
    54  while true
    55  do
    56    if ! pgrep -c -P $pid; then
    57      # all child processes exited
    58      break
    59    fi
    60  
    61    subTasks=$(cat $tasks | grep -v $pid)
    62  
    63    signal=TERM
    64    if [[ $(ms) -gt $ms_end ]]; then
    65      # forcibly kill after the grace period
    66      signal=KILL
    67    fi
    68  
    69    kill -$signal $subTasks 2> /dev/null || true
    70  
    71    sleep 1
    72  done