1、在/etc/init.d/目录下编写脚本,名为nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by ruijie. at 2014.02.26
# if you find any errors on this scripts,please contact ruijie.
# and send mail to ruijie at gmail dot com.
# ruijie.qiao@gmail.com
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ] && netstat -tunpl | grep nginx &> /dev/null;then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog!"
$nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog!"
$nginxd -s stop
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/nginx
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog!"
#kill -HUP `cat ${nginx_pid}`
$nginxd -s reload
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|help}"
exit 1
esac
exit $RETVAL
[root@example ~]# cp nginx /etc/init.d/
[root@example ~]# chmod 755 /etc/init.d/nginx
[root@example ~]# chkconfig --add nginx
3、nginx启动、停止、无间断服务重启
[root@example ~]# service nginx start
[root@example ~]# service nginx stop
[root@example ~]# service nginx reload
添加完以后使用上述命令如果提示No such file or directory 那么可能是因为文件格式不是unix,需要使用命令转化文件格式如下:
两个命令的用法相同,不同的是两个命令使用的环境不一样.
dos2unix在CentOS系列的系统中使用,fromdos则在Ubuntu系列的系统中使用。
用法:dos2unix filename = fromdos filename(经测试dos2unix ubuntu也可以用)
意思是:转换文件格式,将windos系统的文件转换成linux系统可以使用的文件格式,否则文件在linux系统中可能会出现错误,如:文件中显示^m 这样的错误字符。
导致这样的错误出现的原因是,windos环境中的换行符是\r\n,而linux系统中的换行符直接\n就行了。
CENTOS下yum install dos2unix -y
UBUNTU下apt-get install tofrodos ?