Skip to content

Commit 4d9dd94

Browse files
committed
init: LSB compliant init script
With LSB there is no need for having different init script for different distributions. This init script should be fully LSB compliant and should work on all Linux platforms we support. RHEL, (Open)SUSE, Debian and Ubuntu all support at least LSB 3.1
1 parent 7b18b72 commit 4d9dd94

1 file changed

Lines changed: 77 additions & 78 deletions

File tree

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#!/bin/bash
2+
23
### BEGIN INIT INFO
34
# Provides: cloud agent
4-
# Required-Start: $network
5-
# Required-Stop: $network
5+
# Required-Start: $network $local_fs
6+
# Required-Stop: $network $local_fs
67
# Default-Start: 3 4 5
78
# Default-Stop: 0 1 2 6
8-
# X-Interactive: true
9-
# Short-Description: Start/stop apache2 web server
9+
# Short-Description: Start/stop Apache CloudStack Agent
10+
# Description: This scripts Starts/Stops the Apache CloudStack agent
11+
## The CloudStack Agent is a part of the Apache CloudStack project and is used
12+
## for managing KVM-based Hypervisors and performing secondary storage tasks inside
13+
## the Secondary Storage System Virtual Machine.
14+
## JSVC (Java daemonizing) is used for starting and stopping the agent
1015
### END INIT INFO
1116

1217
# Licensed to the Apache Software Foundation (ASF) under one
@@ -26,21 +31,17 @@
2631
# specific language governing permissions and limitations
2732
# under the License.
2833

29-
# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well
30-
3134
. /lib/lsb/init-functions
32-
. /etc/default/rcS
33-
34-
whatami=cloud-agent
3535

36-
# set environment variables
37-
38-
SHORTNAME="$whatami"
39-
PIDFILE=@PIDDIR@/"$whatami".pid
36+
SHORTNAME="cloud-agent"
37+
PIDFILE=@PIDDIR@/"$SHORTNAME".pid
4038
LOCKFILE=@LOCKDIR@/"$SHORTNAME"
4139
LOGFILE=@AGENTLOG@
4240
PROGNAME="Cloud Agent"
4341
CLASS="com.cloud.agent.AgentShell"
42+
PROG="jsvc"
43+
DAEMON="/usr/bin/jsvc"
44+
SHUTDOWN_WAIT="30"
4445

4546
unset OPTIONS
4647
[ -r @SYSCONFDIR@/default/"$SHORTNAME" ] && source @SYSCONFDIR@/default/"$SHORTNAME"
@@ -81,94 +82,92 @@ wait_for_network() {
8182
}
8283

8384
start() {
84-
log_daemon_msg $"Starting $PROGNAME" "$SHORTNAME"
85-
if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
86-
log_progress_msg "apparently already running"
87-
log_end_msg 0
88-
exit 0
89-
fi
90-
if hostname --fqdn >/dev/null 2>&1 ; then
91-
true
92-
else
93-
log_failure_msg "The host name does not resolve properly to an IP address. Cannot start $PROGNAME"
94-
log_end_msg 1
95-
exit 1
96-
fi
97-
98-
wait_for_network
99-
100-
if jsvc -cp "$CLASSPATH" -pidfile "$PIDFILE" $CLASS
101-
RETVAL=$?
102-
then
103-
rc=0
104-
sleep 1
105-
if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
106-
log_failure_msg "$PROG failed to start"
107-
rc=1
108-
fi
109-
else
110-
rc=1
111-
fi
112-
113-
if [ $rc -eq 0 ]; then
114-
log_end_msg 0
115-
else
116-
log_end_msg 1
117-
rm -f "$PIDFILE"
118-
fi
85+
log_daemon_msg "Starting $PROGNAME" "$SHORTNAME"
86+
if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
87+
log_progress_msg "apparently already running"
88+
log_end_msg 0
89+
exit 0
90+
fi
91+
92+
if hostname --fqdn >/dev/null 2>&1 ; then
93+
true
94+
else
95+
log_failure_msg "The host name does not resolve properly to an IP address. Cannot start $PROGNAME"
96+
log_end_msg 1
97+
exit 1
98+
fi
99+
100+
wait_for_network
101+
102+
if start_daemon -p $PIDFILE $DAEMON -cp "$CLASSPATH" -pidfile "$PIDFILE" $CLASS
103+
RETVAL=$?
104+
then
105+
rc=0
106+
sleep 1
107+
if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
108+
log_failure_msg "$PROG failed to start"
109+
rc=1
110+
fi
111+
else
112+
rc=1
113+
fi
114+
115+
if [ $rc -eq 0 ]; then
116+
log_end_msg 0
117+
else
118+
log_end_msg 1
119+
rm -f "$PIDFILE"
120+
fi
119121
}
120122

121123
stop() {
122-
SHUTDOWN_WAIT="30"
123124
count="0"
124125

125-
echo -n $"Stopping $PROGNAME" "$SHORTNAME"
126-
jsvc -pidfile "$PIDFILE" -stop $CLASS
126+
log_daemon_msg "Stopping $PROGNAME" "$SHORTNAME"
127+
killproc -p $PIDFILE $DAEMON
127128

128129
until [ "$count" -gt "$SHUTDOWN_WAIT" ]
129130
do
130-
agentPid=`ps aux|grep [j]svc|grep cloud-agent`
131+
agentPid=$(ps aux|grep [j]svc|grep $SHORTNAME)
131132
if [ "$?" -gt "0" ];then
132133
break
133134
fi
134135
sleep 1
135136
let count="${count}+1"
136137
done
137138

138-
agentPid=`ps aux|grep [j]svc|grep cloud-agent`
139+
agentPid=$(ps aux|grep [j]svc|grep $SHORTNAME)
139140
if [ "$?" -eq "0" ]; then
140-
agentPid=`ps aux|grep [j]svc|awk '{print $2}'`
141-
if [ "$agentPid" != "" ]; then
142-
kill -9 $agentPid
143-
fi
141+
agentPid=$(ps aux|grep [j]svc|awk '{print $2}')
142+
if [ "$agentPid" != "" ]; then
143+
log_warning_msg "$PROG still running, forcing kill"
144+
kill -9 $agentPid
145+
fi
144146
fi
145147

146148
log_end_msg $?
147149
rm -f "$PIDFILE"
148150
}
149151

150-
151-
# See how we were called.
152152
case "$1" in
153-
start)
154-
start
155-
;;
156-
stop)
157-
stop
158-
;;
159-
status)
153+
start)
154+
start
155+
;;
156+
stop)
157+
stop
158+
;;
159+
status)
160160
status_of_proc -p "$PIDFILE" "$PROG" "$SHORTNAME"
161-
RETVAL=$?
162-
;;
163-
restart)
164-
stop
165-
sleep 3
166-
start
167-
;;
168-
*)
169-
echo $"Usage: $whatami {start|stop|restart|status|help}"
170-
RETVAL=3
161+
RETVAL=$?
162+
;;
163+
restart | force-reload)
164+
stop
165+
sleep 3
166+
start
167+
;;
168+
*)
169+
echo "Usage: $0 {start|stop|restart|force-reload|status}"
170+
RETVAL=3
171171
esac
172172

173-
exit $RETVAL
174-
173+
exit $RETVAL

0 commit comments

Comments
 (0)