Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ### BEGIN INIT INFO
- # Provides: datastax-agent
- # Required-Start: $network $local_fs $remote_fs $named $syslog $time
- # Required-Stop: $network $local_fs $remote_fs $syslog
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: DataStax Agent
- ### END INIT INFO
- DESC="DataStax Agent"
- NAME="datastax-agent"
- USER="cassandra"
- MONITOR_NAME="datastax-agent-monitor"
- TAC="datastax-agent.*-standalone.jar"
- PIDFILE="/var/run/datastax-agent/datastax-agent.pid"
- MONITOR_PIDFILE="/var/run/datastax-agent/datastax-agent-monitor.pid"
- LOG="/var/log/datastax-agent/agent.log"
- STARTUP_LOG="/var/log/datastax-agent/startup.log"
- OPSC_SSL_DIR="/var/lib/datastax-agent/ssl"
- OPSC_ADDR_DIR="/var/lib/datastax-agent/conf"
- INIT_VERBOSE=yes
- SCRIPTNAME="$0"
- # Read configuration variable file if it is present
- [ -r /etc/default/$NAME ] && . /etc/default/$NAME
- # Load the VERBOSE setting and other rcS variables
- [ -r /lib/init/vars.sh ] && . /lib/init/vars.sh
- # Define LSB log_* functions.
- # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
- . /lib/lsb/init-functions
- [ -d "/var/run/$NAME" ] || mkdir "/var/run/$NAME"
- [ -d "/var/log/$NAME" ] || mkdir "/var/log/$NAME"
- [ -f "$LOG" ] || touch "$LOG"
- [ -f "$STARTUP_LOG" ] || touch "$STARTUP_LOG"
- [ -e "$PIDFILE" ] || touch $PIDFILE
- chown -RL $USER:$USER "/var/log/$NAME" "/var/run/$NAME"
- is_running()
- {
- if [ -f "$PIDFILE" ]; then
- PID=$(cat "$PIDFILE" 2>/dev/null)
- grep -q "$TAC" "/proc/$PID/cmdline" 2>/dev/null && return 0
- return 1
- fi
- return 3
- }
- do_start()
- {
- # Return
- # 0 if daemon has been started
- # 1 if daemon was already running
- # 2 if daemon could not be started
- is_running && return 1
- nohup /usr/share/datastax-agent/bin/datastax_agent_monitor >> /var/log/datastax-agent/agent.log 2>&1 &
- sleep 1
- export OPSC_SSL_DIR OPSC_ADDR_DIR PID="$PIDFILE"
- nohup start-stop-daemon --start -c $USER --pidfile $PIDFILE --exec /usr/share/datastax-agent/bin/datastax-agent > $STARTUP_LOG 2>&1 &
- return 0
- }
- do_stop()
- {
- # Return
- # 0 if daemon has been stopped
- # 1 if daemon was already stopped
- # 2 if daemon could not be stopped
- # other if a failure occurred
- # stop the monitor process first
- # we don't use --exec /bin/bash because it doesn't seem to always work
- start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
- --pidfile "$MONITOR_PIDFILE"
- MONITOR_RETVAL=$?
- [ $MONITOR_RETVAL -eq 2 ] && return 2 # Couldn't stop
- [ $MONITOR_RETVAL -eq 3 ] && return 3 # Other error
- rm -f "$MONITOR_PIDFILE"
- is_running || return 1
- # then stop the agent
- start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
- --pidfile "$PIDFILE"
- RETVAL=$?
- [ $RETVAL -eq 2 ] && return 2 # Couldn't stop
- [ $RETVAL -eq 3 ] && return 3 # Other error
- rm -f "$PIDFILE"
- return "$RETVAL"
- }
- case "$1" in
- start)
- [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
- do_start
- case "$?" in
- 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
- 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
- esac
- ;;
- stop)
- [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
- do_stop
- case "$?" in
- 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
- 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
- esac
- ;;
- status)
- is_running
- stat=$?
- case "$stat" in
- 0) log_daemon_msg "$DESC $NAME is running" ;;
- 1) log_daemon_msg "could not access pidfile for $DESC $NAME" ;;
- *) log_daemon_msg "$DESC $NAME is not running" ;;
- esac
- exit "$stat"
- ;;
- #reload|force-reload)
- #
- # If do_reload() is not implemented then leave this commented out
- # and leave 'force-reload' as an alias for 'restart'.
- #
- #log_daemon_msg "Reloading $DESC" "$NAME"
- #do_reload
- #log_end_msg $?
- #;;
- restart|force-reload)
- #
- # If the "reload" option is implemented then remove the
- # 'force-reload' alias
- #
- log_daemon_msg "Restarting $DESC" "$NAME"
- do_stop
- case "$?" in
- 0|1)
- do_start
- case "$?" in
- 0) log_end_msg 0 ;;
- 1) log_end_msg 1 ;; # Old process is still running
- *) log_end_msg 1 ;; # Failed to start
- esac
- ;;
- *)
- # Failed to stop
- log_end_msg 1
- ;;
- esac
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
- exit 3
- ;;
- esac
- :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement