应用场景:经常发生某个进程宕机,例如Rsync服务

                图片.png

实验环境:CentOS7

实验脚本:

                

#!/bin/bash
status=$(ps -aux | grep "rsync --daemon" | grep -v "grep" | wc -l)
if [ $status -eq 0 ];
then
systemctl restart rsyncd.service
else
exit 0;
fi

        脚本分析:

                        步骤一: ps -aux | grep "rsync"

                                      发现有多个rsync进程,于是再次删选过滤,找出需要的进程"/usr/bin/rsync --daemon --no-detach"

                        图片.png

                        步骤二:ps -aux | grep "rsync --daemon"

                                     发现找到了需要的rsync进程,但是多了一个“grep”筛选进程,于是再次使用grep -v过滤

                                    图片.png

                        步骤三: ps -aux | grep "rsync --daemon" | grep -v "grep"

                                        终于找到了需要的rsync进程,接下来就需要结合wc计数命令来做判断条件,再与流程控制语句if来编写脚本

                                    图片.png

                        步骤四:ps -aux | grep "rsync --daemon" | grep -v "grep" | wc -l

                                         当rsync服务正在启动时,得到的结果是1,当rsync服务停止时,得到的结果是0

                                    图片.png

                                     图片.png