原力计划【shell脚本】nginx服务管理及存活检测脚本实战 nginx服务器管理及存活检测脚本实战

前言

今天终于敢说自己是csdn万粉博主了,感谢大家的厚爱,我会继续输出更多优质的好文章,一起学习。

座右铭: 先努力让自己发光,再帮助更多的人。

🏠 个人主页:我是沐风晓月
🧑 个人简介:大家好,我是沐风晓月,双一流院校计算机专业,阿里云博客专家 ,csdn万粉博主
😉😉 💕 座右铭:先努力成长自己,再帮助更多的人,一起加油进步
🍺🍺🍺 💕欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信😘

1. 实现的功能

  1. 实现自动管理nginx启动,停止和运行
  2. 实现检测配置文件是否正确
  3. 配置虚拟主机

2. 脚本思路

  1. 指定nginx工作路径:
    nginx=/usr/local/nginx/sbin/nginx

  2. 让用户输入要进行的操作,比如start,stop,status,reload,true
    其中:

    • start 表示启动nginx
    • stop 表示停止服务
    • status 表示查看nginx服务的状态
    • reload 平滑加载配置文件
    • true 判断配置文件是否正确
    • restart 表示设置nginx重新启动,与平滑启动有区别
  3. 判断nginx是否已经启动

基于这个方法,我们可以使用if语句判断执行是否成功,执行成功结果: echo $?=0,否则就不为0;

  1. 使用case语句,指定start,stop,status,reload,true,并进行相关的设置。

3. 脚本代码

#!/bin/bash
NGX=/usr/local/nginx/sbin/nginx
while true
do
read -ep "请输入要执行的命令(start/stop/status/restart/true/reload):" import
case $import in
	#启动nginx选项
	start)
	#先检测nginx是否已经启动
	netstat -nlpt | grep nginx &> /dev/null
	if [ $? -eq 0 ];then
	echo "nginx已经启动!"
	else
	echo "开始启动nginx!"
	$NGX & echo "nginx 启动成功"
	fi
	;;
	#停止nginx运行
	stop)
	$NGX -s stop
	#判断nginx是否已经停止
	if [ $? -eq 0 ];then
	echo "nginx已经停止运行!"
	else
	echo "nginx停止失败,请重试!"
	fi
	;;
	#nginx的状态
	status)
	netstat -nlpt | grep nginx &> /dev/null
	if [ $? -eq 0 ];then
 echo "nginx已经启动!"
 else
 echo "nginx没有运行!"
 fi
	;;
	#重载nginx
	reload)
	$NGX -s reload
	if [ $? -eq 0 ];then
 echo "nginx重载成功!"
 else
 echo "nginx重载失败,请重试!"
 fi
	;;
	restart)
	 $NGX -s stop & echo "nginx服务已经关闭"
	 echo "正在重启服务"
	 $NGX && echo "nginx 已经启动"
	;;
	true)
	$NGX -t & echo "配置文件准确无误" || echo “配置文件出现问题,请进行查证”
	
	*)
	echo "请按提示正确输入!"
	echo -e "t start|stop|restart|status|reloadt"
	;;
esac
done

4. 测试

  • 给脚本执行权限
[root@mufeng41 ~]# vim ng.sh
[root@mufeng41 ~]# chmod +x ng.sh
[root@mufeng41 ~]# ./ng.sh 
请输入要执行的命令(start/stop/status/restart/true/reload):start
nginx已经启动!
请输入要执行的命令(start/stop/status/restart/true/reload):status
nginx已经启动!
请输入要执行的命令(start/stop/status/restart/true/reload):reload
nginx重载成功!
请输入要执行的命令(start/stop/status/restart/true/reload):

5. 脚本优化

使用tput命令,设置终端特性,tput可以设置:

  • 在屏幕上移动光标。
  • 获取终端信息。
  • 设置颜色(背景和前景)。
  • 设置加粗模式。
  • 设置反转模式等等

常见命令示例:

  • tput clear # 清屏
  • tput sc # 保存当前光标位置
  • tput cup 10 13 # 将光标移动到 row col
  • tput civis # 光标不可见
  • tput cnorm # 光标可见
  • tput rc # 显示输出
  • exit 0

例如:

tput cup 5 1 将光标移动到第 5 列 (X) 的第 1 行 (Y)

要更改文本的颜色:

  • setb 选项(用于设置背景颜色)
  • setf 选项(用于设置前景颜色)
0 – Black,黑色
 1 – Red,红色
 2 – Green,绿色
 3 – Yellow,黄色
 4 – Blue,蓝色
 5 – Magenta,品红
 6 – Cyan,青色
 7 – White,白色

例如:

tput setb 6 tput setf 4

接下来,我们把上面的 脚本修改一下,变成有控制面板的。

代码示例:

[root@mufeng41 ~]# cat ng.sh 
#!/bin/bash
NGX=/usr/local/nginx/sbin/nginx
##
# clear the screen
tput clear
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
# Set a foreground colour using ANSI escape
tput setaf 3
echo "沐风nginx管理系统"
tput sgr0
tput cup 5 17
# Set reverse video mode
tput rev
echo "CSDN-我是沐风晓月"
tput sgr0
tput cup 7 15
echo "1. 开启nginx"
tput cup 8 15
echo "2. 关闭nginx"
tput cup 9 15
echo "3. 查看nginx状态"
tput cup 10 15
echo "4. 重新加载配置文件"
# Set bold mode
tput bold
tput cup 12 15
read -p "Enter your choice [1-4] " choice
##
case $choice in
	#启动nginx选项
	1)
	#先检测nginx是否已经启动
	netstat -nlpt | grep nginx &> /dev/null
	if [ $? -eq 0 ];then
	echo "nginx已经启动!"
	else
	echo "开始启动nginx!"
	$NGX & echo "nginx 启动成功"
	fi
	;;
	#停止nginx运行
	2)
	$NGX -s stop
	#判断nginx是否已经停止
	if [ $? -eq 0 ];then
	echo "nginx已经停止运行!"
	else
	echo "nginx停止失败,请重试!"
	fi
	;;
	#nginx的状态
	3)
	netstat -nlpt | grep nginx &> /dev/null
	if [ $? -eq 0 ];then
 echo "nginx已经启动!"
 else
 echo "nginx没有运行!"
 fi
	;;
	#重载nginx
	4)
	$NGX -s reload
	if [ $? -eq 0 ];then
 echo "nginx重载成功!"
 else
 echo "nginx重载失败,请重试!"
 fi
	;;
	*)
	echo "请按提示正确输入!"
	;;
esac
[root@mufeng41 ~]#

执行结果:

总结

在学习服务的过程中,脚本可以随时穿插,方便我们在学习上查缺补漏,及时复习学过的每一个语法。

💕 好啦,这就是今天要分享给大家的全部内容了,我们下期再见!
💕 博客主页:mufeng.blog.csdn.net
💕 本文由沐风晓月原创,首发于CSDN博客
💕 全力以赴,持续学习,不负如来不负卿,喜欢的话记得点赞收藏哦

作者:我是沐风晓月原文地址:https://blog.csdn.net/wisdom_futrue/article/details/129706737

%s 个评论

要回复文章请先登录注册