博客
关于我
Nginx学习笔记3:Shell脚本检测Nginx服务状态
阅读量:218 次
发布时间:2019-02-28

本文共 1295 字,大约阅读时间需要 4 分钟。

前言

nginx 服务启动后,我们需要对其服务状态进行监控,今天学习过程中了解到一段非常有用的小脚本,不仅可以监控Nginx,也可以用来监控其他服务

脚本

A=`ps -C nginx –no-header |wc -l`if [ $A -eq 0 ];then    /usr/local/nginx/sbin/nginx    sleep 2    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then        killall keepalived    fifi

脚本解析

ps -C nginx –no-header

这里涉及ps 的用法,我们一般用ps查看相关进程,一般都是用

ps -ef | grep XX 或 ps -aux| grep XX 的模式
如下:

[root@bogon ~]# ps -ef | grep nginxroot       2248      1  0 19:13 ?        00:00:00 nginx: master process ./nginxnobody     3265   2248  0 20:16 ?        00:00:00 nginx: worker processroot       4791   4742  0 22:05 pts/1    00:00:00 grep --color=auto nginx

但是这样通常都会覆盖一个grep的进程

我们可以用ps -C 或ps -c 的方式, -C 后面接命令的名字 ,如下:

[root@bogon ~]# ps -C nginx   PID TTY          TIME CMD  4983 ?        00:00:00 nginx  4984 ?        00:00:00 nginx

–no-header 就是去挑title,如下:

[root@bogon ~]# ps -C nginx --no-header  4983 ?        00:00:00 nginx  4984 ?        00:00:00 nginx

wc -l 这个很熟悉了,行数计算

所以用这种方式可判断,如果

ps -C nginx --no-header|wc -l

如果值为0,即代表服务未启动如果值为非0,即代表服务已启动

还有一个知识点,killall keepalived 这个是以服务名称杀死进程,日常中也用的挺多的

举一反三

上面那段完整的脚本意思即为:

判断nginx 服务是否启动
如果未启动,执行启动命令
再次判断,如果启动失败,关掉keepalived,进入从节点
关于Nginx的主从我们后面会实操后记录

最重要的一点是,这段脚本不仅可以用来监控Nginx,其他服务也是可以的,模式是一样的,大家可以自行试试

参考连接:

Nginx学习笔记3:Shell脚本检测Nginx服务状态 : https://www.jianshu.com/p/0df46255dd1c

转载地址:http://hzxi.baihongyu.com/

你可能感兴趣的文章
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>