04a0d98232c3f0e70a939c9944c7fe9189873ff9
[sysadmin-cookbook] / recepies / lxc / lxc-watchdog.sh
1 #!/bin/bash -x
2
3 # based on Tony Risinger code from lxc-users
4 # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
5
6 which inotifywait || apt-get install inotify-tools
7
8 name=llin
9
10 #lxc-info -n $name | grep RUNNING && exit
11
12 rootfs=`grep lxc.rootfs /var/lib/lxc/$name/config | cut -d= -f2`
13 echo "$name rootfs $rootfs"
14
15 # fix lxc-stop which remounts ro
16 mount /mnt/llin -o remount,rw
17
18 lxc-start -d -n $name -o /tmp/${name}.log
19
20 while true; do
21         # time of 5 minutes on it JUST IN CASE...
22         vps_utmp=${rootfs}/var/run/utmp
23         inotifywait -qqt 300 ${vps_utmp}
24         if [ $(wc -l < /cgroup/${name}/tasks) -eq 1 ]; then
25
26                 runlevel="$(runlevel ${vps_utmp})"
27                 echo "# $name runlevel $runlevel"
28
29                 case $runlevel in
30                 N*)
31                         # nothing for new boot state
32                 ;;
33                 ??0)
34                         echo "$name halt"
35                         lxc-stop -n "${name}"
36                         break
37                 ;;
38                 ??6)
39                         echo "$name reboot";
40                         lxc-stop -n ${name}
41                         lxc-wait -n ${name} -s STOPPED
42                         mount /mnt/llin -o remount,rw
43                         lxc-start -d -n ${name} -o /tmp/${name}.log
44                 ;;
45                 *)
46                         # make sure vps is still running
47                         state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
48                         [ "$state" = "RUNNING" ] || break
49                 ;;
50                 esac
51         fi
52 done
53