github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/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  path=${GARDEN_CGROUP_PATH}/cpu/instance-$id
    51  tasks=$path/tasks
    52  
    53  while true
    54  do
    55    if ! pgrep -c -P $pid; then
    56      # all child processes exited
    57      break
    58    fi
    59  
    60    subTasks=$(cat $tasks | grep -v $pid)
    61  
    62    signal=TERM
    63    if [[ $(ms) -gt $ms_end ]]; then
    64      # forcibly kill after the grace period
    65      signal=KILL
    66    fi
    67  
    68    kill -$signal $subTasks 2> /dev/null || true
    69  
    70    sleep 1
    71  done