Nginx啟動腳本
Nginx ("engine x") 是一個高性能的HTTP和反向代理伺服器,也是一個 IMAP/POP3/SMTP 代理伺服器。因穩定性、豐富的功能、低資源消耗而聞名。
但Nginx本身不自帶啟動腳本,需要我們手動編寫一份,現在網上所提供的大多數腳本都是有針對行的,可移植性很差。
大多數這樣的腳本依賴于系統中functions函數,但該函數僅在個别系統中存在。
為了使腳本更加通用,以下編寫的腳本可以很輕松的移植到各種Unix、Linux系統中,同時還相容CentOS的chkconfig功能。
測試證明無需修改即可在CentOS、Ubuntu、FreeBSD上測試運作正常。
腳本的思路是通過nginx.pid檔案來判斷程序是否啟動,當然如果你看了http://manual.blog.51cto.com/3300438/932958這篇文章,就可以通過awk過濾端口号判斷程序是否開啟,效果更好。
備注:通過pid判斷服務的啟動與否,可能會導緻stop指令執行結束後pid檔案沒有及時删除(有延遲),而這時進行start啟動服務會報服務已啟動(而不會真的啟動服務)。這種情況會在開啟服務後進行restart指令時出現。是以通過端口判斷服務的開啟與否會更穩定,效果更好...
腳本提供了Nginx所支援的6種程序管理信号中的4種啟動用控制信号,同時額外附件了一個檢視程序狀态的功能。
腳本全文如下:[root@centos6 ~] cat /etc/init.d/nginx
#!/bin/sh
#
# Startup script for the Nginx
# chkconfig: - 88 63
# description: Nginx is a free,open-source,high-performance HTTP Server and reverse proxy.
# program:/usr/local/nginx/sbin/nginx
# config:/usr/local/nginx/conf/nginx.conf
# pidfile:/usr/local/nginx/logs/nginx.pid
# Synopsis:
# nginx [--help] [--version] {start|stop|restart|reload|status|update}
# Define variable
nginx=/usr/local/nginx/sbin/nginx
pidfile=/usr/local/nginx/logs/nginx.pid
PROGRAM=`basename $0`
VERSION=1.0
# Functions
usage(){
echo "Usage: $PROGRAM [--help] [--version] {start|stop|restart|reload|status|update}"
}
version(){
echo "Version:$VERSION"
start(){
if [ -e $pidfile ]
then
echo "Nginx already running..."
else
echo -e "Starting Nginx:\t\t\t\t\t\t\t\c"
/usr/local/nginx/sbin/nginx
echo -e "[ \c"
echo -e "\033[0;32mOK\033[0m\c"
echo -e " ]\c"
echo -e "\r"
fi
stop(){
echo -e "Stopping Nginx:\t\t\t\t\t\t\t\c"
kill -TERM `cat ${pidfile}`
echo "Nginx already stopped..."
reload(){
echo -e "Reloading Nginx:\t\t\t\t\t\t\c"
kill -HUP `cat ${pidfile}`
echo "Nginx is not running..."
status(){
if [ -e $pidfile ]
then
PID=`cat $pidfile`
echo "Nginx (pid $PID) is running..."
else
echo "Nginx is stopped"
fi
update(){
echo -e "Updateing Nginx:\t\t\t\t\t\t\c"
kill -USR2 `cat ${pidfile}`
if [ $# -gt 0 ]
case $1 in
start)
start
;;
stop)
stop
restart)
reload)
reload
status)
status
update)
update
--help)
usage
--version)
version
*)
esac
usage
如果你的系統使用的是CentOS或RedHat的系統,可以通過chkconfig将其設定為開機啟動項。
[root@centos6 ~] chkconfig --add nginx
[root@centos6 ~] chkconfig nginx on
腳本運作效果如圖:
本文轉自丁丁曆險51CTO部落格,原文連結: http://blog.51cto.com/manual/948874,如需轉載請自行聯系原作者