nginx linux脚本,Shell脚本完成nginx启动

编程说明:

之前的一键安装部署nginx脚本中,已经实现了可以使用systemctl命令对nginx服务进行控制,但是在实际环境中,还是有很多nginx不是用脚本装的,所以还是需要用源码安装的控制方式对nginx服务进行控制.

下面将采用case语句编写nginx服务的控制脚本:

源代码:

#!/bin/bash

#chkconfig: 2345 90 98

#功能描述:nginx服务的启动脚本,可作用于Centos6上,Centos7版本也能够向下兼容6版本的脚本

nginx="/usr/local/nginx/sbin/nginx"

pidfile="/usr/local/nginx/logs/nginx.pid"

case $1 in

start)

if [ -f $pidfile ];then

echo -e "\033[91mNginx服务已经正常启动了...\033[0m"

exit

else

$nginx && echo -e "\033[91mNginx服务已经正常启动了...\033[0m"

fi;;

stop)

if [ ! -f $pidfile ];then

echo -e "\033[91mNginx服务已经停止...\033[0m"

exit

else

$nginx -s stop && echo -e "\033[91mNginx服务已经停止...\033[0m"

fi;;

restart)

if [ ! -f $pidfile ];then

echo -e "\033[91mNginx服务已经停止了\033[0m"

echo -e "\033请先运行Nginx服务\033[0m"

exit

else

$nginx -s stop && echo -e "\033[91mNginx服务已经停止了\033[0m"

fi

$nginx && echo -e "\033[91mNginx服务已经正常启动了...\033[0m"

;;

status)

if [ -f $pidfile ];then

echo -e "\033[91mNginx服务已经正常启动了...\033[0m"

else

echo -e "\033[91mNginx服务已经停止了\033[0m"

fi;;

reload)

if [ !  -f $pidfile ];then

echo -e "\033[91mNginx服务已经停止了\033[0m"

exit

else

$nginx -s reload  && echo -e "\033[91mNginx服务已经重新加载配置文件了...\033[0m"

fi;;

*)

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

esac

脚本运行结果:

[root@localhost ~]# chmod +x /root/nginx_console.sh

[root@localhost ~]# /root/nginx_console.sh stop

Nginx服务已经停止...

[root@localhost ~]# /root/nginx_console.sh status

Nginx服务已经停止了

[root@localhost ~]# /root/nginx_console.sh restart

Nginx服务已经停止了

�请先运行Nginx服务

[root@localhost ~]# /root/nginx_console.sh start

Nginx服务已经正常启动了...

[root@localhost ~]# /root/nginx_console.sh restart

Nginx服务已经停止了

Nginx服务已经正常启动了...

[root@localhost ~]# /root/nginx_console.sh reload

Nginx服务已经重新加载配置文件了...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值