shell语句编写nginx的启动脚本

前端之家收集整理的这篇文章主要介绍了shell语句编写nginx的启动脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


Nginx的启动脚本

问题:在预编译安装Nginx时候每次启动时都非常不方便,所以想了下写个Nginx脚本在/etc/init.d目录下可实现service的方式启动、停止、重启这样就方便多了!

[root@cml init.d]#vimNginxd

#!/bin/bash

#chkconfig: 2345 20 80


prog="Nginxd"

Nginx_bin="/usr/local/nginx/sbin/Nginx"

if [ -x $Nginx_bin ]; then##if语句判断是否存在Nginx路径

echo "$Nginx_bin is installled !"

else

echo -n "$Nginx_bin not installed !"

exit 5

fi

start(){

echo -n "starting $prog:"

$Nginx_bin -t ##测试Nginx配置文件

a=$?

if [ $a -eq 0 ]; then###if语句判断上一句是否执行成功

touch /tmp/Nginx.pid ##创建一个Nginx.pid文件后启动Nginx

$Nginx_bin

echo "please check your Nginx config"

exit 10

}

stop(){

echo -n $"stopping $prog:"

$Nginx_bin -s stop ##停止Nginx

if [ $a -eq 0 ]; then ##判断执行上一句成功就删除Nginx.pid文件输出信息

rm -rf /tmp/Nginx.pid

echo "success stop Nginx"

echo "check your Nginx command"

exit 11

case $1 in ##

start)

start

;;

stop)

stop

reload|restart)

sleep 2

start

*)

echo "Usage:$0{start|stop|reload/restart}"

;;

esac

验证:


[root@cml init.d]# service Nginxd start

/usr/local/Nginx/sbin/Nginx is installled !

starting Nginxd:Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok

Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful

Nginx: [emerg] bind() to 0.0.0.0:80 Failed (98: Address already in use)

Nginx: [emerg] still could not bind()

[root@cml init.d]# service Nginxd stop

stopping Nginxd:success stop Nginx

[root@cml init.d]# service Nginxd restart

Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful

原文链接:https://www.f2er.com/bash/390998.html

猜你在找的Bash相关文章