如下爲nginx啓動腳本,如有不妥之處,歡迎指出。
#!/bin/bash
#chkconfig: 2345 80 90
#description:auto_runsource /etc/profile
[ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1ng_path=/usr/local/lnmp/nginx
[ -f ${ng_path}/sbin/nginx ] || exit 1
[ -f ${ng_path}/conf/nginx.conf ] || exit 1debug=0
function tof(){
if [ ${status} -ne 0 ] ; then
debug=1
fi
}case $1 in
start)
$ng_path/sbin/nginx 2>/dev/null
status=$?
tof
;;
stop)
$ng_path/sbin/nginx -s stop 2>/dev/null
status=$?
tof
;;
restart)
$ng_path/sbin/nginx -s stop 2>/dev/null && \
$ng_path/sbin/nginx 2>/dev/null
status=$?
tof
;;
reload)
$ng_path/sbin/nginx -s reload 2>/dev/null
status=$?
tof
;;
status)
sta=`netstat -anp | awk '/^tcp.*:80\>/{print $NF}'|grep nginx `
if [ "$sta" == "" ];thenecho 'nginx is not running.'
else
echo "$sta"| awk -F '/' '{if($2 == "nginx"){print "SUCCESS! ",$2," running (",$1,")"}else {print "nginx is not running."}}'
fi
debug=2
;;
check)
$ng_path/sbin/nginx -t
debug=2
;;
*)
echo 'USAGE: $0 <start|stop|restart|reload|status|check>'
debug=3
;;esac
if [ $debug -eq 0 ] ;then
action "$0 $1" /bin/true
elif [ $debug -eq 1 ];then
action "$0 $1" /bin/false
fi
上述腳本中在查詢狀態時容易獲取不到相應信息,需要根據實際情況作出相應修改
下面附加在rhel7.X/centos7.X nginx的啓動腳本 ---20170813追加
[Unit]
Description=nginx service
After=network.target[Service]
Type=forking
ExecStart=/usr/local/nginx-1.10.2/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/local/nginx-1.10.2/sbin/nginx -s reload
ExecStop=/usr/local/nginx-1.10.2/sbin/nginx -s stop
PrivateTmp=true[Install]
WantedBy=multi-user.target