Advertisement
Guest User

Untitled

a guest
Oct 7th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.64 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # (C) Copyright Canonical 2011,2012
  4.  
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9.  
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Lesser General Public License for more details.
  14.  
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with this library; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18.  
  19. set -e
  20.  
  21. usage() {
  22.     echo "usage: lxc-shutdown -n name"
  23.     echo "  Cleanly shut down a container."
  24. #    echo "  -w: wait for shutdown to complete."
  25. #    echo "  -r: reboot (ignore -w)."
  26. #    echo "  -t timeout: wait at most timeout seconds (implies -w), then kill"
  27. #    echo "              the container."
  28. }
  29.  
  30. alarm() {
  31.     trap 'exit 0' TERM
  32.     pid=$1
  33.     timeout=$2
  34.     sleep $timeout
  35.     kill $pid
  36. }
  37.  
  38. dolxcstop()
  39. {
  40.     echo "Calling lxc-stop on $lxc_name"
  41.     lxc-stop -n $lxc_name
  42.     exit 0
  43. }
  44.  
  45. usage_err() {
  46.     [ -n "$1" ] && echo "$1" >&2
  47.     usage
  48.     exit 1
  49. }
  50.  
  51. optarg_check() {
  52.     [ -n "$2" ] || usage_err "option '$1' requires an argument"
  53. }
  54.  
  55. timeout="-1"
  56.  
  57. reboot=0
  58. dowait=0
  59.  
  60. while [ $# -gt 0 ]; do
  61.     opt="$1"
  62.     shift
  63.     case "$opt" in
  64.     -h|--help)
  65.         usage
  66.         exit 0
  67.         ;;
  68.     -n|--name)
  69.         optarg_check $opt "$1"
  70.         lxc_name=$1
  71.         shift
  72.         ;;
  73. #    -w|--wait)
  74. #        dowait=1
  75. #        ;;
  76. #    -r|--reboot)
  77. #        reboot=1
  78. #        ;;
  79. #    -t|--timeout)
  80. #        optarg_check $opt "$1"
  81. #        timeout=$1
  82. #        dowait=1
  83. #        shift
  84. #        ;;
  85.     --)
  86.         break;;
  87.     -?)
  88.         usage_err "unknown option '$opt'"
  89.         ;;
  90.     -*)
  91.         # split opts -abc into -a -b -c
  92.         set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
  93.         ;;
  94.     *)
  95.         usage_err "unknown option '$opt'"
  96.         exit 1
  97.         ;;
  98.     esac
  99. done
  100.  
  101. if [ -z "$lxc_name" ]; then
  102.     echo "no container name specified"
  103.     usage
  104.     exit 1
  105. fi
  106.  
  107. if [ "$(id -u)" != "0" ]; then
  108.    echo "This command has to be run as root"
  109.    exit 1
  110. fi
  111.  
  112. #which lxc-info > /dev/null 2>&1 || { echo "lxc-info not found."; exit 1; }
  113. which lxc-wait > /dev/null 2>&1 || { echo "lxc-wait not found."; exit 1; }
  114.  
  115. #pid=`lxc-info -n $lxc_name -p 2>/dev/null | awk '{ print $2 }'`
  116. pid=`ps aux | grep lxc-start | grep $lxc_name |awk '{print $2}'`
  117. pidCheck=`ps aux | grep lxc-start | grep $lxc_name |awk '{print $2}' | wc -l`
  118. #echo $pid
  119. #echo $pidCheck
  120.  
  121. if [ $pidCheck -eq 0 ]; then
  122.     echo "$lxc_name is not running"
  123.     #exit 1
  124. else
  125.     kill -s INT $pid
  126.     echo "waiting STOPPED status $lxc_name container"
  127.     lxc-wait -n $lxc_name -s STOPPED
  128.     echo "Container $lxc_name has shut down"
  129.     #lxc-stop -n $lxc_name #!!!commsnds ne rabotaet
  130.     kill $pid
  131. fi
  132.  
  133. #if [ $dowait -eq 0 ]; then
  134. #    exit 0
  135. #fi
  136.  
  137. #if [ $timeout != "-1" ]; then
  138. #    trap dolxcstop EXIT
  139. #    alarm $$ $timeout 2>/dev/null &
  140. #    alarmpid=$!
  141. #fi
  142.  
  143. #while ! lxc-info -n $lxc_name -s STOPPED; do
  144. #    sleep 1
  145. #done
  146.  
  147. #if [ $timeout != "-1" ]; then
  148. #    trap - EXIT
  149. #    # include subprocesses; otherwise, we may have to wait until sleep completes:w
  150. #    # if called from a non-interactive context
  151. #    kill $alarmpid $(ps --no-headers --ppid $alarmpid -o pid) 2>/dev/null || :
  152. #fi
  153.  
  154. #echo "Container $lxc_name has shut down"
  155.  
  156. #exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement