Skip to content

统一操作NMP的Bash脚本

Published: at 00:00
说明
我的电脑安装了Nginx, PHP, MySQL的环境,我之前每次都是手动启动他们,要启动这三个就是输入三条命令。
于是弄了这个脚本统一的操作它们。
源码
#!/usr/bin/env bash

# 这个脚本只是为了方便手动操作MySQL, PHP-FPM, Nginx
#	只实现了 start, stop, restart
#	作者: nate_yhz
#	邮箱: nate_yhz (AT) outlook.com

#启动
start () {
	echo "starting MySQL"
	/opt/mysql/bin/mysql.server start
	echo "starting PHP"
	php-fpm -D
	echo "starting Nginx"
	nginx
}

#停止
stop () {
	echo "stoping MySQL, PHP, Nginx"
	/opt/mysql/bin/mysql.server stop
	echo "stoping PHP"
	ps aux | grep php-fpm | grep -v grep | awk '{print $2}' | xargs kill -9
	echo "stoping Nginx"
	ps aux | grep nginx | grep -v grep | awk '{print $2}' | xargs kill -9

}

#重启
restart () {
	echo "restarting MySQL"
	/opt/mysql/bin/mysql.server restart
	echo "restarting PHP"
	ps aux | grep php-fpm | grep -v grep | awk '{print $2}' | xargs kill -9
	php-fpm -D
	echo "restarting Nginx"
	ps aux | grep nginx | grep -v grep | awk '{print $2}' | xargs kill -9
	nginx
}

# 检查是否是root用户运行
check_root () {
	if [[ $EUID -ne 0 ]]; then
		echo "Must be a root user!!" 2>&1
		exit 1
	else
		return 0
	fi
}


# 用法
usage () {
	basename=`basename "$0"`
	echo "Usage: $basename {start|stop|restart} "
	exit 1
}

check_root
mode=$1

case "$mode" in
	'start')
		start
		;;
	'stop')
		stop
		;;
	'restart')
		restart
		;;
	*)
		usage
		;;
esac

exit 0