修改resin的自动拉起

#! /bin/sh
#
# See contrib/init.resin for /etc/rc.d/init.d startup script
#
# resin.sh can be called like apachectl
#
# resin.sh         -- execs resin in the foreground
# resin.sh start   -- starts resin in the background
# resin.sh stop    -- stops resin
# resin.sh restart -- restarts resin
#
# resin.sh will return a status code if the wrapper detects an error, but
# some errors, like bind exceptions or Java errors, are not detected.
#
# To install, you'll need to configure JAVA_HOME and RESIN_HOME and
# copy contrib/init.resin to /etc/rc.d/init.d/resin.  Then
# use "unix# /sbin/chkconfig resin on"
#JAVA_HOME
JAVA_HOME=/usr/local/java
export JAVA_HOME
#本脚本的绝对路径
EXEC_START="/usr/local/snsconnector/snsconnector.sh"

#执行脚本
RESIN="bin/httpd.sh"
#执行多个resin的多路径
EXEC="/usr/local/snsconnector/snsapi/resin3 /usr/local/snsconnector/testsns/resin3"

array=($EXEC)
length=${#array[@]}
echo $length
for ((i=0; i<$length; i++))
do
    echo ${array[$i]}
done

#####################################################
# Find a PID for the pid file
#####################################################

for ((i=0; i<$length; i++))
do
	RESIN_HOME=${array[$i]}
	SNS_PIDS="$SNS_PIDS $RESIN_HOME/snsconnector.pid "
done

pids=($SNS_PIDS)
size=${#pids[@]}

echo $size
for ((i=0; i<$size; i++))
do
    echo ......${pids[$i]}
done


#
SNS_TAG=snstag


if test -n "${JAVA_HOME}"; then
  if test -z "${JAVA_EXE}"; then
    JAVA_EXE=$JAVA_HOME/bin/java
  fi
fi  

#
# trace script and simlinks to find the wrapper
#
if test -z "${RESIN_HOME}"; then
  script=`/bin/ls -l $0 | awk '{ print $NF; }'`

  while test -h "$script"
  do
    script=`/bin/ls -l $script | awk '{ print $NF; }'`
  done

  bin=`dirname $script`
  RESIN_HOME="$bin/.."
fi  

running()
{
    [ -f $1 ] || return 1
    PID=$(cat $1)
    echo ==========$PID==========
    ps -p $PID >/dev/null 2>/dev/null || return 1
    return 0
}

usage()
{
    echo "Usage: $0 {start|stop|status} [ CONFIGS ... ] "
    exit 1
}

[ $# -gt 0 ] || usage


ACTION=$1
MAINTE_CHECK=$2


#####################################################
# Get current script belongs to User and Group.
#####################################################
[ -z ${SCRIPT_NAME} ] && SCRIPT_NAME=httpd.sh
if [ -f ${RESIN_HOME}/bin/${SCRIPT_NAME} ]
then
	CURRENT_GROUP="$(echo `ls -l ${RESIN_HOME}/bin/${SCRIPT_NAME}` | awk '{print $4}')";
	CURRENT_USER="$(echo `ls -l ${RESIN_HOME}/bin/${SCRIPT_NAME}` | awk '{print $3}')";
else
	echo "** ERROR: Current start service script is:"${RESIN_HOME}/bin/${SCRIPT_NAME}
    echo "** ERROR: The SNS start file is not exist!"
fi
setCrontab()
{
    echo "setCrontab...";
    crontab_tmp=$(mktemp -p "${RESIN_HOME}/bin/")

		if [ x$CURRENT_USER != x$(whoami) ] 
		then
			echo "* * * * * su - $CURRENT_USER -c '"${EXEC_START} start $SNS_TAG" '> /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 10; su - $CURRENT_USER -c '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 20; su - $CURRENT_USER -c '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 30; su - $CURRENT_USER -c '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 40; su - $CURRENT_USER -c '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 50; su - $CURRENT_USER -c '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
		elif [ x$CURRENT_USER == x$(whoami) ]
		then
			echo "* * * * * '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 10; '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 20; '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 30; '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 40; '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
			echo "* * * * * sleep 50; '"${EXEC_START} start $SNS_TAG"' > /dev/null 2>&1" >> "${crontab_tmp}"
		fi
	
		tail "${crontab_tmp}"
	crontab "${crontab_tmp}"
	rm -f ${crontab_tmp}
	return 0
}


case "$ACTION" in
  start)

	for ((i=0; i<$length; i++))
	do
		RESIN_HOME=${array[$i]}
		RUN_CMD=$RESIN_HOME/$RESIN
		SNS_PID=${pids[$i]}
		echo -n "$RESIN_HOME Starting snsconnector: "
		if [ x$MAINTE_CHECK == x$SNS_TAG ] && [ -f "$RESIN_HOME/$SNS_TAG" ]
		then
			echo "Maintenance period,can't start sns service!"
			exit 1
		fi
		if [ -f $SNS_PID ]
		then
			if running $SNS_PID
			then
				echo "Already Running!!"
				echo "snsconnector running pid is:"`cat $RESIN_HOME/snsconnector.pid`
#exit 1
continue
			else
				# dead pid file - remove
				rm -f $SNS_PID
			fi
		fi
	    
		if [ x$CURRENT_USER != x ] && [ x$CURRENT_USER != x$(whoami) ] 
		then
			touch $SNS_PID
			chown $CURRENT_USER:$CURRENT_GROUP $SNS_PID
			su - $CURRENT_USER -c "
			$RUN_CMD > /dev/null 2>&1 &
			PID=\$!
			disown \$PID
			echo \$PID > $SNS_PID"
			echo pid=$PID
			echo sns_pid=$SNS_PID
		elif [ x$CURRENT_USER == x$(whoami) ]
		then
			echo ===$RUN_CMD===
			touch $SNS_PID
			eval "
			$RUN_CMD > /dev/null 2>&1 &
			PID=\$!
			disown \$PID
			echo \$PID > $SNS_PID"
			echo pid=$PID
                        echo sns_pid=$SNS_PID
		fi 
		sleep 5
		if running $SNS_PID
		then
			rm -f $RESIN_HOME/$SNS_TAG
			echo "OK"
			echo "pid is `cat $SNS_PID 2>/dev/null`"
			echo "STARTED AT `date`"
		else
			echo "FAILED!"
		fi
	done
	setCrontab;
	exit 1
        ;;

  stop)

	for ((i=0; i<$length; i++))
	do
		RESIN_HOME=${array[$i]}
		SNS_PID=${pids[$i]}
		echo -n "$RESIN_HOME Stopping snsconnector... "
		if running $SNS_PID
		then
			PID=`cat $RESIN_HOME/$SNS_PID 2>/dev/null`
			TIMEOUT=60
			while running $SNS_PID && [ $TIMEOUT -gt 0 ]
			do
				kill $PID 2>/dev/null
				echo -n "."
				sleep 1
				let TIMEOUT=$TIMEOUT-1
			done
		
			[ $TIMEOUT -gt 0 ] || kill -9 $PID 2>/dev/null
			rm -f $SNS_PID
			echo OK
		else
			echo $SNS_PID snsconnector service already stopped,stop failed!
		fi
	        touch $RESIN_HOME/$SNS_TAG
	done
	exit 1
        ;;

  status)

	for ((i=0; i<$length; i++))
	do
	    RESIN_HOME=${array[$i]}
	    SNS_PID=${pids[$i]}
		    if running $SNS_PID
		    then
			PID=`cat $SNS_PID 2>/dev/null`
			echo "snsconnector(pid $PID) is running!"
		    else
			rm -f $SNS_PID
			echo "snsconnector has stoped!"
		    fi
	done
    exit 1
    ;;

*)
        usage
    ;;
esac

exit 0

你可能感兴趣的:(shell,crontab,resin,自动拉起)