-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxc-graceful
executable file
·53 lines (45 loc) · 1.65 KB
/
lxc-graceful
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
########################################################################
#
# script written by Nelson Pascoal - used to gracefully shut down a
# lxc container instance.
#
########################################################################
GREP=`which grep`
PS=`which ps`
AWK=`which awk`
KILL=`which kill`
WC=`which wc`
LXCINFO=`which lxc-info`
container=$1
conf="/etc/lxc/$container.conf"
if [ -e $conf ]
then
running=`$LXCINFO -n $container | $GREP STOPPED | $WC -l`
if [ $running -eq 0 ]
then
# discover pid for lxc-start process
lxcid=`$PS -ef | $GREP $conf | $GREP -v $GREP | $AWK '{print $2}'`
# use lxc-start process pid to discover correct init process
initid=`$PS -ef | $GREP $lxcid | $GREP -v $GREP | $GREP init | $AWK '{print $2}'`
# send shutdown signal to init process
$KILL -SIGPWR $initid
running=`$LXCINFO -n $container | $GREP STOPPED | $WC -l`
echo -n "Waiting for container $container to stop."
while [ $running -eq 0 ]
do
sleep 1
echo -n "."
running=`$LXCINFO -n $container | $GREP STOPPED | $WC -l`
done
echo
echo "Container $container stopped successfully"
exit 0
else
echo "Container $container is currently is status STOPPED - container shutdown terminated"
exit 2
fi
else
echo "Container config file $conf not found - unable to initiate container shutdown"
exit 1
fi