用initd服务化nginx,支持centos6,7和ubuntu
vim /etc/init.d/nginx
#! /bin/bash
#2019-02-12
PATH=/data/sbin:/data/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/data/nginx/sbin/$NAME
CONFIGFILE=/data/nginx/conf/$NAME.conf
PIDFILE=/data/nginx/logs/$NAME.pid
if [ -s /usr/sbin/ss ]; then
StatBin=/usr/sbin/ss
else
StatBin=/usr/bin/netstat
fi
case "$1" in
start)
echo -n "Starting $NAME... "
if $StatBin -tnpl | grep -q nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
$0 force-quit
else
echo " done"
fi
;;
status)
if $StatBin -tnpl | grep -q nginx; then
PID=`pidof nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped."
exit 0
fi
;;
force-quit|kill)
echo -n "Terminating $NAME... "
if ! $StatBin -tnpl | grep -q nginx; then
echo "$NAME is is stopped."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if $StatBin -tnpl | grep -q nginx; then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
exit 1
;;
esac
chmod +x /etc/init.d/nginx
chkconfig nginx on