mqaction-v1.sh

#!/bin/bash
#20171025

#Service_name=(Users Orders Capital Sysaccount Products Trusteeship) 
#Users_port=9611
#Orders_port=9615
#Capital_port=9617
#Sysaccount_port=9618
#Products_port=9619
#Trusteeship_port=9620
declare -A Service
Service=([Users]=9611 [Orders]=9615 [Capital]=9617 [Sysaccount]=9618 [Products]=9619 [Trusteeship]=9620)

# source function library
. /etc/rc.d/init.d/functions

action()
{
	does=$1
	server=$2
	SPORT=${Service[$server]}
	is_exists=`netstat -anltp| grep $SPORT`
	
	if [ x != x"$is_exists" ];then
		if [ $does == 'start' ];then
			echo "The $server already start!"
		elif [ $does == 'stop' ];then
			lsof -i:$SPORT| grep php| grep -v grep | awk '{print $2}'|xargs kill -9
		elif [ $does == 'restart' ];then
			action stop $server
			action start $server
		elif [ $does == 'status' ];then
			echo "The $server running..."
		fi
	else
		if [ $does == 'start' ];then
			cd /data/htdocs/v/ServiceModule/$server 
			nohup /usr/local/php7/bin/php mqconsumer.php >/dev/null 2>&1 &
			#cd /data/htdocs/v/ServiceModule/$server && nohup /usr/local/php7/bin/php mqconsumer.php >/dev/null &
		elif [ $does == 'stop' ];then
			echo "The $server already stop!"
		elif [ $does == 'restart' ];then
			action stop $server
			action start $server
		elif [ $does == 'status' ];then
			echo "The $server stopping..."
		fi
	fi
}

cycle()
{
	doing=$1
	sername=$2
	if [ $sername == 'all' ];then
		for key in $(echo ${!Service[*]});do
			action $doing $key
		done
	else
		action $doing $sername
	fi
}

cycle $1 $2