| Current Path : /etc/rubrik/ |
| Current File : //etc/rubrik/start_stop.sh |
if [ -f "/etc/init.d/functions" ]
then
source /etc/init.d/functions
fi
exists_start_stop() {
command -v "start-stop-daemon" > /dev/null 2>&1
}
exists_daemon() {
command -v "daemon" > /dev/null 2>&1
}
exists_startproc() {
command -v "startproc" > /dev/null 2>&1
}
exists_killproc() {
command -v "killproc" > /dev/null 2>&1
}
exists_pgrep() {
command -v "pgrep" > /dev/null 2>&1
}
exists_pkill() {
command -v "pkill" > /dev/null 2>&1
}
exists_pidof() {
command -v "pidof" > /dev/null 2>&1
}
get_pid_from_name() {
local PROCESS_NAME=$1
if exists_pidof; then
# -o %PPID : omit the parent PID
# -s : return only one pid
pidof -s -o%PPID $PROCESS_NAME
elif exists_pgrep; then
# We prefer pgrep first because -n will
# give latest pid
pgrep -n -u root -f /usr/bin/rubrik/$PROCESS_NAME
else
# last resort
ps -ef | grep $PROCESS_NAME | grep -v grep | awk '{print $2}'
fi
}
check_and_kill_proc() {
local PROCESS_NAME=$1
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
pids=`get_pid_from_name $PROCESS_NAME`
if [ ! -z "${pids}" ]; then
for pid in $pids; do
kill -9 "$pid" > /dev/null 2>&1
RETVAL="$?"
done
else
if exists_pkill; then
# As last resort we use pkill and process name
# We do not expect multilple processes of same
# agent to run on host at a time so the pattern
# should match exact agent process
pkill -f /usr/bin/rubrik/${PROCESS_NAME}
fi
fi
}
stop_with_ssd() {
# TODO(soham) Do we need start-stop-daemon at all?
# If file does not exist, nothing was installed
local PROCESS_NAME=$1
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
if [ -e $PIDFILE ]
then
pid=`cat $PIDFILE 2>/dev/null`
kill -9 $pid > /dev/null 2>&1
RETVAL="$?"
# If 1 is returned, process is not there, which
# should not be an error for stop
[ $RETVAL = 1 ] && RETVAL=0
# If kill succeeded or process does not exist, remove
# file
[ $RETVAL = 0 ] && rm -f "$PIDFILE"
fi
# Make sure no instance of agent process is running.
check_and_kill_proc $PROCESS_NAME
}
start_with_ssd() {
local PROCESS_NAME=$1
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
stop_with_ssd $PROCESS_NAME
# Create the pidfile if it doesn't exist.
if [ ! -f $PIDFILE ]; then
touch $PIDFILE
fi
start-stop-daemon --start --quiet --pidfile $PIDFILE \
--exec $DAEMON -- $DAEMON_OPTS
RETVAL=$?
pid=`get_pid_from_name $PROCESS_NAME`
if [ -n "$pid" ]; then
echo $pid > "$PIDFILE"
fi
}
stop_with_killproc() {
local PROCESS_NAME=$1
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
# If file does not exist, nothing was installed
if [ -e $PIDFILE ]
then
killproc -p "$PIDFILE" "$DAEMON" >/dev/null 2>&1
RETVAL="$?"
# If 7 is returned, process is not there, which
# should not be an error for stop
[ $RETVAL = 7 ] && RETVAL=0
# If kill succeeded or process does not exist, remove
# file
[ $RETVAL = 0 ] && rm -f "$PIDFILE"
fi
# Make sure no instance of agent process is running.
check_and_kill_proc $PROCESS_NAME
}
start_with_daemon() {
local PROCESS_NAME=$1
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
stop_with_killproc $PROCESS_NAME
daemon --user="root" --pidfile="$PIDFILE" "$DAEMON $DAEMON_OPTS"
RETVAL=$?
pid=`get_pid_from_name $PROCESS_NAME`
if [ -n "$pid" ]; then
echo $pid > "$PIDFILE"
fi
}
start_with_startproc() {
local PROCESS_NAME=$1
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
stop_with_killproc $PROCESS_NAME
startproc -u 0 -p="$PIDFILE" $DAEMON $DAEMON_OPTS
RETVAL=$?
pid=`get_pid_from_name $PROCESS_NAME`
if [ -n "$pid" ]; then
echo $pid > "$PIDFILE"
fi
}
start() {
local PROCESS_NAME=$1
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
echo "Starting daemon: "$PROCESS_NAME
if exists_start_stop; then
start_with_ssd $PROCESS_NAME
elif exists_startproc; then
start_with_startproc $PROCESS_NAME
elif exists_daemon; then
start_with_daemon $PROCESS_NAME
else
echo "Cannot find appropriate daemon management binary"
return 1
fi
}
stop() {
local PROCESS_NAME=$1
local CHILD_PROCESS_NAME=$2
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
echo "Stopping daemon: "$PROCESS_NAME
if exists_start_stop; then
stop_with_ssd $PROCESS_NAME
if [ ! -z $CHILD_PROCESS_NAME ]; then
# Make sure no instance of child process is running.
stop_with_ssd $CHILD_PROCESS_NAME
fi
elif exists_killproc; then
stop_with_killproc $PROCESS_NAME
if [ ! -z $CHILD_PROCESS_NAME ]; then
# Make sure no instance of child process is running.
stop_with_killproc $CHILD_PROCESS_NAME
fi
else
echo "Cannot find appropriate daemon management binary"
return 1
fi
}
RETVAL=0
COMMAND=$1
PROCESS_NAME=$2
CHILD_PROCESS_NAME=$3
if [ -z $PROCESS_NAME ]; then
echo "Failed to get process name"
exit 1
fi
case $COMMAND in
start)
start $PROCESS_NAME
echo
;;
stop)
stop $PROCESS_NAME $CHILD_PROCESS_NAME
echo
;;
restart)
stop $PROCESS_NAME $CHILD_PROCESS_NAME
echo
start $PROCESS_NAME
RETVAL=$?
echo
;;
status)
echo -n "Status daemon: "$PROCESS_NAME
if [ -f $PIDFILE ]
then
if [ -e /proc/`cat $PIDFILE 2>/dev/null` ]
then
echo " [UP]"
RETVAL=0
else
echo " [DOWN]"
RETVAL=3
fi
else
echo " [DOWN]"
RETVAL=3
fi
;;
*)
echo "Usage: "$COMMAND" {start|stop|restart|status}"
RETVAL=1
esac
exit $RETVAL