mqaction-v2.sh

#!/bin/bash

# Edit by ZeroC
# 20180407

. /etc/init.d/functions
name=$1
action=$2
basedir=/data/htdocs/v/$name

startmq(){
	cd $basedir
	statusmq
	if [ $? -eq 0 ];then
		#echo -e "\033[33;1m$name MQ监听启动\033[0m"
		nohup /usr/local/php70/bin/php mqconsumer.php > /dev/null 2>&1 &
		echo -e "\033[33;1mInfo\033[0m:\033[32;1m$name MQ监听启动完成\033[0m"
	elif [ $? -eq 1 ];then
		echo -e "\033[33;1mWarning\033[0m:\033[33;1m$name MQ监听已启动\033[0m"
	fi
}

stopmq(){
	cd $basedir
	statusmq
	if [ $? -eq 1 ];then
		#echo -e "\033[33;1m$name MQ监听停止\033[0m"
		/usr/local/php70/bin/php mqconsumer.php stop > /dev/null
		ps aux | grep php-ps| grep -v grep | awk '{print $2}'| xargs kill -9 > /dev/null 2>&1
		echo -e "\033[33;1mInfo\033[0m:\033[32;1m$name MQ监听停止完成\033[0m"
	elif [ $? -eq 0 ];then
		echo -e "\033[33;1mWarning\033[0m:\033[33;1m$name MQ监听已停止\033[0m"
	fi
}

restartmq(){
	stopmq
	startmq
}

statusmq(){
	cd $basedir
	result1=`/usr/local/php70/bin/php mqconsumer.php status`
	result2=`netstat -anltp | grep php-ps| grep -v grep`
	if [ $result1 -eq 0 ] && [ x == x"$result2" ];then
		echo -e "\033[33;1mStatus\033[0m:\033[32;1m$name MQ监听已停止\033[0m"
		return 0
	elif [ $result1 -eq 1 ] && [ x != x"$result2" ];then
		echo -e "\033[33;1mStatus\033[0m:\033[32;1m$name MQ监听在运行\033[0m"
		return 1
	fi
}

case $action in
	start)
		startmq
		;;
	stop)
		stopmq
		;;
	status)
		statusmq
		;;
	restart)
		restartmq
		;;
	*)
		echo -e "\033[33;1mi\t参数异常!\nUsage: bash $0 $name (start|stop|restart|status)\033[0m"
		;;
esac