PHP+Nginx+MongoDB启动脚本

每次开机都要手工一个一个启动,nginx,php,mongodb,太麻烦了,写了个脚本,但是MongoDB启动会输出日志,所以不是很完美,会显示日志。
/root/webserver

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
export LC_ALL=C
# Check if user is root
if [ $(id -u) != "0" ]; then
printf "Error: You must be root to run this script!\n"
exit 1
fi

NGINXDAEMON=/home/server/nginx-1.4.1/sbin/nginx
NGINXPIDFILE=/home/server/nginx-1.4.1/logs/nginx.pid
PHPFPMDAEMON=/home/server/php-5.4.15/sbin/php-fpm
PHPFPMPID=/home/server/php-5.4.15/var/run/php-fpm.pid
MONGODBDAEMON=/home/server/mongodb-linux-i686-2.4.3/bin/mongod
MONGODBCONF=/home/server/mongodb-linux-i686-2.4.3/mongodb.conf

function_start()
{
printf "Starting Nginx+PHP+Mongodb...\n"
if [ -f $NGINXPIDFILE ]; then
printf "Nginx is runing!\n"
else
$NGINXDAEMON
fi
if [ -f $PHPFPMPID ]; then
printf "PHP-FPM is runing!\n"
else
$PHPFPMDAEMON
fi
lsof -i:27017 && printf "MongoDB is runing!\n" || $MONGODBDAEMON --config $MONGODBCONF
}

function_stop()
{
printf "Stoping Nginx+PHP...\n"
if [ -f $NGINXPIDFILE ]; then
kill -QUIT `cat $NGINXPIDFILE`
else
printf "Nginx program is not runing!\n"
fi

if [ -f $PHPFPMPID ]; then
kill -INT `cat $PHPFPMPID`
else
printf "PHP-FPM program is not runing!\n"
fi

lsof -i:27017 && killall mongod || printf "MongoDB is not runing!\n"
}

function_status()
{
if [ -f $NGINXPIDFILE ]; then
printf "Nginx is runing!\n"
else
printf "Nginx is stop!\n"
fi
if [ -f $PHPFPMPID ]; then
printf "PHP-FPM is runing!\n"
else
printf "PHP-FPM is stop!\n"
fi

lsof -i:27017 && printf "MongoDB is runing!\n" || printf "MongoDB is stop!\n"

}

if [ "$1" = "start" ]; then
function_start
elif [ "$1" = "stop" ]; then
function_stop
elif [ "$1" = "restart" ]; then
function_stop
function_start
elif [ "$1" = "status" ]; then
function_status
else
printf "Usage: /root/webserver {start|stop|restart|status}\n"
fi

chmod +x /root/webserver
添加开机启动:
vi /etc/rc.local
添加
/root/webserver start
重启生效

© 2013, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

发表评论

电子邮件地址不会被公开。 必填项已用*标注