* if citadel doesn't come down by sendcommand in 10 seconds, send it a TERM, after...
[citadel.git] / citadel / debian / citadel.init
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          citadel
4 # Required-Start:    $local_fs $remote_fs
5 # Required-Stop:     $local_fs $remote_fs
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: control citadel server start at boot time
9 # Description:       control citadel server start at boot time
10 ### END INIT INFO
11
12 # Author: Wilfried Goesgens <citadel@outgesourced.org>
13
14 RUNDIR=/var/run/citadel
15 PATH=/sbin:/usr/sbin:/bin:/usr/bin
16 DESC="Citadel Groupware "
17 NAME=citserver
18 DAEMON=/usr/sbin/$NAME
19 PIDFILE=$RUNDIR/citadel.pid
20 DAEMON_ARGS=" -d -x3 -lmail -t/dev/null"
21 SCRIPTNAME=/etc/init.d/citadel
22 SENDCOMMAND=/usr/sbin/sendcommand
23
24 # Exit if the package is not installed
25 [ -x "$DAEMON" ] || exit 0
26
27 # Read configuration variable file if it is present
28 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
29
30 MODERN=
31
32 # Load the VERBOSE setting and other rcS variables
33 if test -f /lib/init/vars.sh ; then
34     . /lib/init/vars.sh
35     MODERN=1
36 fi
37 # Define LSB log_* functions.
38 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
39 if test -f /lib/lsb/init-functions; then
40     . /lib/lsb/init-functions
41     MODERN=1
42 fi
43
44 #
45 # Function that starts the daemon/service
46 #
47 do_start()
48 {
49         # for ubuntu: check our volatile dirs.
50         if test ! -d $RUNDIR; then
51             mkdir -p $RUNDIR
52         fi
53         # Return
54         #   0 if daemon has been started
55         #   1 if daemon was already running
56         #   2 if daemon could not be started
57         if $DAEMON \
58                 $DAEMON_ARGS ; then
59             return 0
60         else
61             return 2
62         fi
63 }
64
65 #
66 # Function that stops the daemon/service
67 #
68 do_stop()
69 {
70         # Return
71         #   0 if daemon has been stopped
72         #   1 if daemon was already stopped
73         #   2 if daemon could not be stopped
74         #   other if a failure occurred
75         if $SENDCOMMAND "DOWN" >/dev/null 2>&1 ; then
76             rm -f $PIDFILE
77             count=1;
78             while test -S /var/run/citadel/citadel.socket; do 
79                 count=$(($count+1))
80                 sleep 1
81                 echo -n "."
82                 if test "$count" == "10"; then
83                     killall citserver
84                 fi
85                 if test "$count" == "20"; then
86                     killall -9 citserver
87                     rm -rf /var/run/citadel/*
88                 fi
89             done
90                 return 0
91         else
92             rm -f $PIDFILE
93             return 2
94         fi
95 }
96
97 #
98 # Function that sends a SIGHUP to the daemon/service
99 #
100 do_reload() {
101         do_stop
102         do_start
103         return 0
104 }
105
106 case "$1" in
107   start)
108         if test -n "$MODERN"; then
109             [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
110         else
111             echo "Starting $DESC" "$NAME"
112         fi
113
114         do_start
115         if test -n "$MODERN"; then
116             case "$?" in
117                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
118                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
119             esac
120         fi
121         ;;
122   stop)
123         if test -n "$MODERN"; then
124             [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
125         else
126             echo "Stopping $DESC" "$NAME"
127         fi
128         do_stop
129         if test -n "$MODERN"; then
130             case "$?" in
131                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
132                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
133             esac
134         fi
135         ;;
136   restart|force-reload)
137         if test -n "$MODERN"; then
138             log_daemon_msg "Restarting $DESC" "$NAME"
139         else
140             echo "Restarting $DESC" "$NAME"
141         fi
142
143         do_stop
144
145         if test -n "$MODERN"; then
146             case "$?" in
147                 0|1)
148                     do_start
149                     case "$?" in
150                         0) log_end_msg 0 ;;
151                         1) log_end_msg 1 ;; # Old process is still running
152                         *) log_end_msg 1 ;; # Failed to start
153                     esac
154                     ;;
155                 *)
156                 # Failed to stop
157                     log_end_msg 1
158                     ;;
159             esac
160         else
161             do_start
162         fi
163         ;;
164   *)
165         echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
166         exit 3
167         ;;
168 esac
169
170 exit 0