1. 程式人生 > >用shell編寫nginx腳本的啟動,關閉,重加載

用shell編寫nginx腳本的啟動,關閉,重加載

shell bash case

#!/bin/bash                 ----默認執行shell方式
#chkconfig: 2345 10 80        ----加入到開機執行的方式
path="/usr/local/nginx/sbin/nginx"        ----源代碼安裝nginx之後的啟動路徑
name="nginxd"   
test=0
log=/tmp/nginxd.log                ----nginx的日誌
DATE=`date "+%F  %H:%M:%S"`        ----獲得系統時間命令



#判斷nginx是否已經安裝或者判斷源碼安裝之後啟動的路徑是否存在。
if [ ! -x $path ];then
	echo  -n "${path} not installed!"
	exit 10
fi

#啟動選項,對執行nginx進行啟動,啟動成功或者失敗都會輸出提醒。
start(){
	echo -n " starting $name ;"
	$path -t
	test=$?
	if [ $test -eq 0 ];then
	touch /tmp/nginx.pid
	$path
	echo "$DATE and $path is starting" >> $log
else
	echo "please check you config"
	exit 20
	fi
}


#停止nginx服務,並把輸出的信息重定向到指定的日誌文件。
stop(){
	echo -n "stopping $name ;"
	ps -ef |grep nginx | awk ‘{print $2}‘ | xargs kill -9
	test=0
	if [ $test -eq 0 ] ;then
	rm -rf /tmp/nginx.pid 
	echo "$DATE and $path is stop" >> $log
	fi
}


#重加載選項,將服務重新加載並識別服務是否正常運行。
reload(){
	echo -n "reloading $name ;"
#	ps -ef |grep nginx | awk ‘{print $2}‘ | xargs kill -9
	$path -t
	test=$?
	if [ $test -eq 0 ];then
	touch /tmp/reload.pid
	echo "$DATE and $path is reloading " >> $log
	rm -rf /tmp/reload.pid
else
	echo "please check you config"
	exit 20
	fi
}


#整個腳本使用case方式來進行調用,當執行該腳本時會輸出相關信息提醒。
case "$1" in
	start) start;;
	stop) stop;;
	reload) reload;;
	*) echo "/usr/local/nginx/sbin/nginx start|stop|reload !";;
esac




######---退出shell之後,執行以下操作---######

1.對文件加權限
chmod +x nginxd

2.對文件執行
bash nginxd [start|stop|reload]


本文出自 “leoheng” 博客,請務必保留此出處http://leoheng.blog.51cto.com/12202141/1955762

用shell編寫nginx腳本的啟動,關閉,重加載