From 4d9dd94b53cb1e57bb3d160fff2c2eeb7abfc3fb Mon Sep 17 00:00:00 2001 From: Wido den Hollander Date: Wed, 25 Jul 2012 14:06:22 +0200 Subject: [PATCH] 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 --- .../ubuntu/SYSCONFDIR/init.d/cloud-agent.in | 149 +++++++++--------- 1 file changed, 74 insertions(+), 75 deletions(-) diff --git a/agent/distro/ubuntu/SYSCONFDIR/init.d/cloud-agent.in b/agent/distro/ubuntu/SYSCONFDIR/init.d/cloud-agent.in index 4f6f04dd4d7..b98efcac659 100755 --- a/agent/distro/ubuntu/SYSCONFDIR/init.d/cloud-agent.in +++ b/agent/distro/ubuntu/SYSCONFDIR/init.d/cloud-agent.in @@ -1,12 +1,17 @@ #!/bin/bash + ### BEGIN INIT INFO # Provides: cloud agent -# Required-Start: $network -# Required-Stop: $network +# Required-Start: $network $local_fs +# Required-Stop: $network $local_fs # Default-Start: 3 4 5 # Default-Stop: 0 1 2 6 -# X-Interactive: true -# Short-Description: Start/stop apache2 web server +# Short-Description: Start/stop Apache CloudStack Agent +# Description: This scripts Starts/Stops the Apache CloudStack agent +## The CloudStack Agent is a part of the Apache CloudStack project and is used +## for managing KVM-based Hypervisors and performing secondary storage tasks inside +## the Secondary Storage System Virtual Machine. +## JSVC (Java daemonizing) is used for starting and stopping the agent ### END INIT INFO # Licensed to the Apache Software Foundation (ASF) under one @@ -26,21 +31,17 @@ # specific language governing permissions and limitations # under the License. -# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well - . /lib/lsb/init-functions -. /etc/default/rcS -whatami=cloud-agent - -# set environment variables - -SHORTNAME="$whatami" -PIDFILE=@PIDDIR@/"$whatami".pid +SHORTNAME="cloud-agent" +PIDFILE=@PIDDIR@/"$SHORTNAME".pid LOCKFILE=@LOCKDIR@/"$SHORTNAME" LOGFILE=@AGENTLOG@ PROGNAME="Cloud Agent" CLASS="com.cloud.agent.AgentShell" +PROG="jsvc" +DAEMON="/usr/bin/jsvc" +SHUTDOWN_WAIT="30" unset OPTIONS [ -r @SYSCONFDIR@/default/"$SHORTNAME" ] && source @SYSCONFDIR@/default/"$SHORTNAME" @@ -81,53 +82,53 @@ wait_for_network() { } start() { - log_daemon_msg $"Starting $PROGNAME" "$SHORTNAME" - if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then - log_progress_msg "apparently already running" - log_end_msg 0 - exit 0 - fi - if hostname --fqdn >/dev/null 2>&1 ; then - true - else - log_failure_msg "The host name does not resolve properly to an IP address. Cannot start $PROGNAME" - log_end_msg 1 - exit 1 - fi + log_daemon_msg "Starting $PROGNAME" "$SHORTNAME" + if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then + log_progress_msg "apparently already running" + log_end_msg 0 + exit 0 + fi - wait_for_network + if hostname --fqdn >/dev/null 2>&1 ; then + true + else + log_failure_msg "The host name does not resolve properly to an IP address. Cannot start $PROGNAME" + log_end_msg 1 + exit 1 + fi - if jsvc -cp "$CLASSPATH" -pidfile "$PIDFILE" $CLASS - RETVAL=$? - then - rc=0 - sleep 1 - if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then - log_failure_msg "$PROG failed to start" - rc=1 - fi - else - rc=1 - fi + wait_for_network - if [ $rc -eq 0 ]; then - log_end_msg 0 - else - log_end_msg 1 - rm -f "$PIDFILE" - fi + if start_daemon -p $PIDFILE $DAEMON -cp "$CLASSPATH" -pidfile "$PIDFILE" $CLASS + RETVAL=$? + then + rc=0 + sleep 1 + if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then + log_failure_msg "$PROG failed to start" + rc=1 + fi + else + rc=1 + fi + + if [ $rc -eq 0 ]; then + log_end_msg 0 + else + log_end_msg 1 + rm -f "$PIDFILE" + fi } stop() { - SHUTDOWN_WAIT="30" count="0" - echo -n $"Stopping $PROGNAME" "$SHORTNAME" - jsvc -pidfile "$PIDFILE" -stop $CLASS + log_daemon_msg "Stopping $PROGNAME" "$SHORTNAME" + killproc -p $PIDFILE $DAEMON until [ "$count" -gt "$SHUTDOWN_WAIT" ] do - agentPid=`ps aux|grep [j]svc|grep cloud-agent` + agentPid=$(ps aux|grep [j]svc|grep $SHORTNAME) if [ "$?" -gt "0" ];then break fi @@ -135,40 +136,38 @@ stop() { let count="${count}+1" done - agentPid=`ps aux|grep [j]svc|grep cloud-agent` + agentPid=$(ps aux|grep [j]svc|grep $SHORTNAME) if [ "$?" -eq "0" ]; then - agentPid=`ps aux|grep [j]svc|awk '{print $2}'` - if [ "$agentPid" != "" ]; then - kill -9 $agentPid - fi + agentPid=$(ps aux|grep [j]svc|awk '{print $2}') + if [ "$agentPid" != "" ]; then + log_warning_msg "$PROG still running, forcing kill" + kill -9 $agentPid + fi fi log_end_msg $? rm -f "$PIDFILE" } - -# See how we were called. case "$1" in - start) - start - ;; - stop) - stop - ;; - status) + start) + start + ;; + stop) + stop + ;; + status) status_of_proc -p "$PIDFILE" "$PROG" "$SHORTNAME" - RETVAL=$? - ;; - restart) - stop - sleep 3 - start - ;; - *) - echo $"Usage: $whatami {start|stop|restart|status|help}" - RETVAL=3 + RETVAL=$? + ;; + restart | force-reload) + stop + sleep 3 + start + ;; + *) + echo "Usage: $0 {start|stop|restart|force-reload|status}" + RETVAL=3 esac -exit $RETVAL - +exit $RETVAL \ No newline at end of file