Bug#404018: Break out of the `while (loop)'-loop when no socket can be opened.
[collectd.git] / debian / collectd.init.d
1 #!/bin/bash
2 #
3 # collectd      Initscript for collectd
4 #               http://collectd.org/
5 # Authors:      Florian Forster <octo@verplant.org>
6 #               Sebastian Harl <sh@tokkee.org>
7 #
8
9 set -e
10
11 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
12 DESC="Statistics collection daemon"
13 NAME=collectd
14 DAEMON=/usr/sbin/$NAME
15 SCRIPTNAME=/etc/init.d/$NAME
16 ARGS=""
17
18 CONFIGDIR=/etc/collectd
19 # for backward compatibility
20 FALLBACKCONF=/etc/collectd.conf
21
22 # Gracefully exit if the package has been removed.
23 test -x $DAEMON || exit 0
24
25 if [ -r /etc/default/$NAME ]
26 then
27         . /etc/default/$NAME
28 fi
29
30 #
31 #       Function that starts the daemon/service.
32 #
33 d_start() {
34         i=0
35         
36         if [ ! -d "$CONFIGDIR" ]
37         then
38                 if [ -e "$FALLBACKCONF" ]
39                 then
40                         $DAEMON -C "$FALLBACKCONF" 2> /dev/null
41                 else
42                         echo ""
43                         echo "This package is not configured yet. Please refer"
44                         echo "to /usr/share/doc/collectd/README.Debian for"
45                         echo "details."
46                         echo ""
47                         exit 0
48                 fi
49         else
50                 for FILE in `ls $CONFIGDIR/*.conf 2>/dev/null`
51                 do
52                         NAME=`basename "$FILE" .conf`
53
54                         if [ $i == 0 ]
55                         then
56                                 echo -n " ("
57                         else
58                                 echo -n ", "
59                         fi
60                         
61                         $DAEMON -C "$FILE" 2> /dev/null
62                         if [ $? == 0 ]
63                         then
64                                 echo -n "$NAME"
65                         else
66                                 echo -n "$NAME failed"
67                         fi
68
69                         i=$(($i+1))
70                 done
71
72                 if [ $i == 0 ]
73                 then
74                         echo -n "[no config found]"
75                         exit 1
76                 else
77                         echo -n ")"
78                 fi
79         fi
80 }
81
82 #
83 #       Function that stops the daemon/service.
84 #
85 d_stop() {
86         start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
87 }
88
89 case "$1" in
90   start)
91         echo -n "Starting $DESC: $NAME"
92         d_start
93         echo "."
94         ;;
95   stop)
96         echo -n "Stopping $DESC: $NAME"
97         d_stop
98         echo "."
99         ;;
100   restart|force-reload)
101         echo -n "Restarting $DESC: $NAME"
102         d_stop
103         sleep 1
104         d_start
105         echo "."
106         ;;
107   *)
108         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
109         exit 1
110         ;;
111 esac
112
113 exit 0
114
115 # vim: syntax=sh noexpandtab sw=8 ts=8 :