Bug 10624: add 'status' option switch for the packages init script
[koha.git] / debian / koha-common.init
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          koha-common
4 # Required-Start:    $remote_fs
5 # Required-Stop:     $remote_fs
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: Start Zebra server for each Koha instance
9 # Description:       For each enabled Koha instance on this host,
10 #                    as listed by "koha-list --enabled", start a Zebra
11 #                    server (using koha-start-zebra).
12 ### END INIT INFO
13
14 # Author: Lars Wirzenius <lars@catalyst.net.nz>
15
16 # Do NOT "set -e"
17
18 # PATH should only include /usr/* if it runs after the mountnfs.sh script
19 PATH=/sbin:/usr/sbin:/bin:/usr/bin
20 DESC="Koha ILS"
21 NAME="koha-common"
22 SCRIPTNAME=/etc/init.d/$NAME
23
24 # Exit if the package is not installed
25 [ -x /usr/sbin/koha-start-zebra ] || exit 0
26
27 # Read configuration variable file if it is present
28 if [ -r /etc/default/$NAME ]; then
29     # Debian / Ubuntu
30     . /etc/default/$NAME
31 elif [ -r /etc/sysconfig/$NAME ]; then
32     # RedHat / SuSE
33     . /etc/sysconfig/$NAME
34 fi
35
36 # Load the VERBOSE setting and other rcS variables
37 . /lib/init/vars.sh
38
39 # Define LSB log_* functions.
40 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
41 . /lib/lsb/init-functions
42
43 #
44 # Function that starts the daemon/service
45 #
46 do_start()
47 {
48     # We insure all required directories exist, including disabled ones.
49     koha-create-dirs $(koha-list)
50     koha-start-zebra $(koha-list --enabled)
51     koha-start-sip $(koha-list --enabled)
52 }
53
54 #
55 # Function that stops the daemon/service
56 #
57 do_stop()
58 {
59     # We stop everything, including disabled ones.
60     koha-stop-zebra $(koha-list) || true
61     koha-stop-sip $(koha-list) || true
62 }
63
64 #
65 # Function that sends a SIGHUP to the daemon/service
66 #
67 do_reload() {
68     koha-restart-zebra $(koha-list --enabled)
69     koha-stop-sip $(koha-list) || true
70     koha-start-sip $(koha-list --enabled)
71 }
72
73 #
74 # Function that checks zebrasrv is running for the specified instance
75 #
76 is_zebra_running()
77 {
78     local instancename=$1
79
80     if daemon --name="$instancename-koha-zebra" \
81             --user="$instancename-koha.$instancename-koha" \
82             --running ; then
83         return 0
84     else
85         return 1
86     fi
87 }
88
89 #
90 # Function that checks SIP server is running for the specified instance
91 #
92 is_sip_running()
93 {
94     local instancename=$1
95
96     if daemon --name="$instancename-koha-sip" \
97             --pidfiles="/var/run/koha/$instancename" \
98             --user="$instancename-koha.$instancename-koha" \
99             --running ; then
100         return 0
101     else
102         return 1
103     fi
104 }
105
106 #
107 # Function that shows the status of the zebrasrv daemon for
108 # enabled instances
109 #
110 zebra_status()
111 {
112     for instance in $(koha-list --enabled); do
113
114         log_daemon_msg "Zebra server running for instace $instance"
115
116         if is_zebra_running $instance ; then
117             log_end_msg 0
118         else
119             log_end_msg 1
120         fi
121     done
122 }
123
124 #
125 # Function that shows the status of the SIP server daemon for
126 # enabled instances
127 #
128 sip_status()
129 {
130     for instance in $(koha-list --enabled --sip); do
131
132         log_daemon_msg "SIP server running for instace $instance"
133
134         if is_sip_running $instance ; then
135             log_end_msg 0
136         else
137             log_end_msg 1
138         fi
139     done
140 }
141
142 case "$1" in
143   start)
144         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
145         do_start
146         case "$?" in
147                 0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
148                 *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
149         esac
150         ;;
151   stop)
152         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
153         do_stop
154         case "$?" in
155                 0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
156                 *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
157         esac
158         ;;
159   restart|force-reload)
160         #
161         # If the "reload" option is implemented then remove the
162         # 'force-reload' alias
163         #
164         log_daemon_msg "Restarting $DESC" "$NAME"
165         do_stop
166         case "$?" in
167           0)
168                 do_start
169                 case "$?" in
170                         0) log_end_msg 0 ;;
171                         *) log_end_msg 1 ;; # Failed to start
172                 esac
173                 ;;
174           *)
175                 # Failed to stop
176                 log_end_msg 1
177                 ;;
178         esac
179         ;;
180   status)
181         zebra_status
182         sip_status
183         ;;
184   *)
185     echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
186         exit 3
187         ;;
188 esac
189
190 :