天天看点

【Linux中高级运维: 第53天——Shell编程】第4章:编写服务进程管理脚本

作业:

1、判断nginx/mysql是否活着,如果没有活着就邮件报警,每三分钟执行一次。

2、进程是否存在,端口是否开启,curl/wget访问URL

3、写一个rsync/sersync/nginx的启动脚本。
/etc/init.d/rsync {start|stop|restart}      
1、服务器本地端口监控:

过滤端口然后转成行数。

netstat -lntup|grep nginx|wc -l(推荐)
ss -lntup|grep nginx|wc -l
lsof -i :80|wc      
2、远端端口监控:

看返回的行数,为1正常,否则不正常。

nmap 127.0.0.1 -p 80|grep open|wc -l(推荐) 
echo -e "\n"|telnet 127.0.0.1 80 2>/dev/null|grep Connected|wc      

看返回值,为0正常。

[root@web01 ~]# nc -z 127.0.0.1 80
[root@web01 ~]# echo $?      

nc的控制参数不少,常用的几个参数如下所列:

1) -l
用于指定nc将处于侦听模式。指定该参数,则意味着nc被当作server,侦听并接受连接,而非向其它地址发起连接。
2) -p <port>
暂未用到(老版本的nc可能需要在端口号前加-p参数,下面测试环境是centos6.6,nc版本是nc-1.84,未用到-p参数)
3) -s 
指定发送数据的源IP地址,适用于多网卡机 
4) -u
 指定nc使用UDP协议,默认为TCP
5)      
3、客户端模拟监控:看返回值或者看返回字符串
wget -q 127.0.0.1 &>/dev/null (推荐)
echo $?

curl 127.0.0.1 &>/dev/null
echo $?      

看返回字符串是否为www7

[root@web01 /etc/nginx/conf.d]# curl -s 127.0.0.1 2>/dev/null(推荐)      
环境准备:
[root@oldboy ~]# touch /etc/rsyncd.conf
[root@oldboy ~]# 
[root@oldboy ~]# rsync --daemon
[root@oldboy ~]# lsof -i :873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   1690 root    4u  IPv4  14658      0t0  TCP *:rsync (LISTEN)
rsync   1690 root    5u  IPv6  14659      0t0  TCP *:rsync (LISTEN)
[root@oldboy ~]# pkill rsync
[root@oldboy ~]# lsof -i :873      
启动脚本:

3、写一个rsync/sersync/nginx的启动脚本。

/etc/init.d/rsync {start|stop|restart}      
1)启动:rsync --daemon
2)停止:pkill rsync
   kill `cat /var/run/rsyncd.pid`
   
3)监控手段:netstat -lntup|grep rsync|wc -l
/etc/init.d/rsyncd {start|stop|restart}      
[root@oldboy init.d]# cat rsyncd
#!/bin/bash

if [ "$1" = "start"  ]
then
   if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
   then
       :
   else
       rsync --daemon
   fi
elif [ "$1" = "stop"  ]
then
    if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
    then
        kill `cat /var/run/rsyncd.pid`
    else
        echo "Failed to stop rsync Unit rsync not loaded."
        exit 1
    fi
elif [ "$1" = "restart"  ]
then
    if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
    then
        kill `cat /var/run/rsyncd.pid`
    fi
   sleep 2

   if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
   then
       :
   else
       rsync --daemon
   fi
else
    echo "Usage;$0
fi      

C7:systemctl

c6:管理开机自启动服务,用的chkconfig

[root@oldboy init.d]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[root@oldboy init.d]# 
[root@oldboy init.d]# 
[root@oldboy init.d]# 
[root@oldboy init.d]# chkconfig --list
abrt-ccpp        0:off  1:off  2:off  3:on  4:off  5:on  6:off
abrtd            0:off  1:off  2:off  3:on  4:off  5:on  6:off
acpid            0:off  1:off  2:on  3:on  4:on  5:on  6:off
atd              0:off  1:off  2:off  3:on  4:on  5:on  6:off
auditd          0:off  1:off  2:on  3:on  4:on  5:on  6:off
blk-availability  0:off  1:on  2:on  3:on  4:on  5:on  6:off
cpuspeed        0:off  1:on  2:on  3:on  4:on  5:on  6:off
crond            0:off  1:off  2:on  3:on  4:on  5:on  6:off
haldaemon        0:off  1:off  2:off  3:on  4:on  5:on  6:off
htcacheclean    0:off  1:off  2:off  3:off  4:off  5:off  6:off
httpd            0:off  1:off  2:off  3:off  4:off  5:off  6:off
ip6tables        0:off  1:off  2:on  3:on  4:on  5:on  6:off
iptables        0:off  1:off  2:on  3:on  4:on  5:on  6:off
irqbalance      0:off  1:off  2:off  3:on  4:on  5:on  6:off
kdump            0:off  1:off  2:off  3:on  4:on  5:on  6:off
lvm2-monitor    0:off  1:on  2:on  3:on  4:on  5:on  6:off
mdmonitor        0:off  1:off  2:on  3:on  4:on  5:on  6:off
messagebus      0:off  1:off  2:on  3:on  4:on  5:on  6:off
netconsole      0:off  1:off  2:off  3:off  4:off  5:off  6:off
netfs            0:off  1:off  2:off  3:on  4:on  5:on  6:off
network          0:off  1:off  2:on  3:on  4:on  5:on  6:off
nfs-rdma        0:off  1:off  2:off  3:off  4:off  5:off  6:off
ntpd            0:off  1:off  2:off  3:off  4:off  5:off  6:off
ntpdate          0:off  1:off  2:off  3:off  4:off  5:off  6:off
postfix          0:off  1:off  2:on  3:on  4:on  5:on  6:off
psacct          0:off  1:off  2:off  3:off  4:off  5:off  6:off
quota_nld        0:off  1:off  2:off  3:off  4:off  5:off  6:off
rdisc            0:off  1:off  2:off  3:off  4:off  5:off  6:off
rdma            0:off  1:on  2:on  3:on  4:on  5:on  6:off
restorecond      0:off  1:off  2:off  3:off  4:off  5:off  6:off
rngd            0:off  1:off  2:off  3:off  4:off  5:off  6:off
rsyslog          0:off  1:off  2:on  3:on  4:on  5:on  6:off
saslauthd        0:off  1:off  2:off  3:off  4:off  5:off  6:off
smartd          0:off  1:off  2:off  3:off  4:off  5:off  6:off
sshd            0:off  1:off  2:on  3:on  4:on  5:on  6:off
svnserve        0:off  1:off  2:off  3:off  4:off  5:off  6:off
sysstat          0:off  1:on  2:on  3:on  4:on  5:on  6:off
udev-post        0:off  1:on  2:on  3:on  4:on  5:on  6:off
[root@oldboy init.d]# chkconfig crond status
chkconfig version 1.3.49.5 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.

usage:   chkconfig [--list] [--type <type>] [name]
         chkconfig --add <name>
         chkconfig --del <name>
         chkconfig --override <name>
         chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
         
[root@oldboy init.d]# chkconfig --list crond
crond            0:off  1:off  2:on  3:on  4:on  5:on  6:off
[root@oldboy init.d]# chkconfig crond off
[root@oldboy init.d]# chkconfig --list crond
crond            0:off  1:off  2:off  3:off  4:off  5:off  6:off
[root@oldboy init.d]# chkconfig crond on
[root@oldboy init.d]# chkconfig --list crond
crond            0:off  1:off  2:on  3:on  4:on  5:on  6:off
[root@oldboy init.d]# chkconfig --level 3 crond off
[root@oldboy init.d]# chkconfig --list crond
crond            0:off  1:off  2:on  3:off  4:on  5:on  6:off
[root@oldboy init.d]# chkconfig --level 45 crond off
[root@oldboy init.d]# chkconfig --list crond
crond            0:off  1:off  2:on  3:off  4:off  5:off  6:off
[root@oldboy init.d]# chkconfig --list rsyncd
service rsyncd does not support chkconfig




       For example, random.init has these three lines:
       # chkconfig: 2345 20 80
       # description: Saves and restores system entropy pool for \
       #              higher quality random number generation.
       This says that the random script should be started in levels 2, 3, 4, and 5, that its start  priority
       should  be  20,  and  that its stop priority should be 80.  You should be able to figure out what the
       description says; the \ causes the line to be continued.  The extra space in      
[root@oldboy init.d]# ll /etc/init.d/rsyncd
-rwxr-xr-x. 1 root root 763 May 30 09:55 /etc/init.d/rsyncd
[root@oldboy init.d]# chmod +x /etc/init.d/rsyncd
[root@oldboy init.d]# chkconfig --add rsyncd
[root@oldboy init.d]# chkconfig --list rsyncd
rsyncd          0:off  1:off  2:on  3:on  4:on  5:on  6:off
[root@oldboy init.d]# head -3 rsyncd
#!/bin/bash
# chkconfig: 2345 21 81
# description: startup rsync scripts

[root@oldboy init.d]# chkconfig rsyncd off
[root@oldboy init.d]# chkconfig --list rsyncd
rsyncd          0:off  1:off  2:off  3:off  4:off  5:off  6:off

[root@oldboy init.d]# chkconfig rsyncd on
[root@oldboy init.d]# chkconfig --list rsyncd
rsyncd          0:off  1:off  2:on  3:on  4:on  5:on  6:off
[root@oldboy init.d]# ls /etc/rc.d/rc3.d/|grep rsync
S21rsyncd

[root@oldboy init.d]# ls /etc/rc.d/rc3.d/-l|grep rsync
ls: cannot access /etc/rc.d/rc3.d/-l: No such file or directory

[root@oldboy init.d]# ls /etc/rc.d/rc3.d/ -l|grep rsync
lrwxrwxrwx. 1 root root 16 May 30 09:58 S21rsyncd -> ../init.d/rsyncd

[root@oldboy init.d]# chkconfig rsyncd off
[root@oldboy init.d]# ls /etc/rc.d/rc3.d/ -l|grep rsync
lrwxrwxrwx. 1 root root 16 May 30 09:59 K81rsyncd -> ../init.d/rsyncd      
centOS7:用systemctl实现开机自启动管理
[root@web01 ~]# cat /usr/lib/systemd/system/rsyncd_oldboy.service
[Unit]
Description=Rsync service
After=network.target
 
[Service]
Type=forking
PIDFile=/var/run/rsyncd.pid
ExecStart=/etc/rc.d/init.d/rsyncd start
ExecReload=/etc/rc.d/init.d/rsyncd restart
ExecStop=/etc/rc.d/init.d/rsyncd stop
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target


[root@web01 ~]# cat /usr/lib/systemd/system/rsyncd
rsyncd_oldboy.service  rsyncd.service         [email protected]        rsyncd.socket      
    
[root@web01 ~]# cat /usr/lib/systemd/system/rsyncd.service 
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf

[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"

[Install]
WantedBy=multi-user.target


systemctl start rsyncd_oldboy
systemctl enable rsyncd_oldboy
netstat -lntup|grep      
今日作业:写一个编译安装的nginx的启动脚本。
C6:用chkconfig实现开机自启动管理。
/etc/init.d/nginxd {start|stop|restart}

C7:用systemctl实现开机自启动管理。
systemctl {start|stop|restart}      
今日作业:写一个编译安装的nginx的启动脚本。
C6:用chkconfig实现开机自启动管理。
/etc/init.d/nginxd {start|stop|restart}      
【Linux中高级运维: 第53天——Shell编程】第4章:编写服务进程管理脚本
【Linux中高级运维: 第53天——Shell编程】第4章:编写服务进程管理脚本
【Linux中高级运维: 第53天——Shell编程】第4章:编写服务进程管理脚本
C7:用systemctl实现开机自启动管理。
systemctl {start|stop|restart} nginxd                                             [  确定  ]      
【Linux中高级运维: 第53天——Shell编程】第4章:编写服务进程管理脚本
CentOS7.5下开发systemctl管理的自定义Rsync启动服务程序

1.1 systemctl知识简介

从CentOS7 Linux开始,系统里的网络服务启动已经从传统的service改成了systemctl(一个systemd工具,主要负责控制systemd系统和服务管理器。),管理开机自启动的命令也从chkconfig改为了systemctl,由systemctl一个命令代替了CentOS7以前系统中的service和chkconfig两个命令。

系统服务的脚本也从传统的路径的/etc/init.d(/etc/rc.d/init.d/),改到了/usr/lib/systemd(除此之外还有/etc/systemd/system),需要自启动运行的程序,一般存在这个系统服务目录下,即:/usr/lib/systemd/system目录,每一个服务以“服务名.service”结尾,该文件的内容一般分为3部分:即[Unit]、[Service]和[Install],

1.2 systemctl管理的sshd服务配置介绍

下面是系统中sshd服务配置及解释说明。

[root@oldboy ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]   #<==对该系统服务描述及说明模块。
Description=OpenSSH server daemon            #<==描述性说明。
Documentation=man:sshd(8) man:sshd_config(5) #<==文档列表说明。
After=network.target sshd-keygen.service     #<==服务依赖类别说明。
Wants=sshd-keygen.service     #<==可选的依赖服务。
 
[Service]   #<==系统服务的运行参数设置模块
Type=notify #<==服务类型,可选有forking、notify、simple等。
EnvironmentFile=/etc/sysconfig/sshd  #<==环境变量等的配置文件。
ExecStart=/usr/sbin/sshd -D $OPTIONS #<==服务的启动程序。
ExecReload=/bin/kill -HUP $MAINPID   #<==重启程序。
KillMode=process
Restart=on-failure
RestartSec=42s
 


[Install] #<==服务安装的相关设置。
WantedBy=multi-user.target   #<==这里为设置多用户级别。可为空格分隔的列表, 表示在使用 systemctl enable 启用此单元时, 将会在对应的目录设置对应文件的软连接。      

更多说明,可参考systemd.unit、system.service文档,此不细述,都掌握了意义也不大,可以写出启动脚本即可。

1.3 systemctl管理的rsyncd_oldboy服务配置说明

下面是我们人为配置的rsyncd服务配置及详细解释说明,因为系统里已经配置好了rsyncd.service,因此这里使用rsyncd_oldboy.service以区别。

[root@oldboy ~]# cat /usr/lib/systemd/system/rsyncd_oldboy.service
[Unit]
Description=Rsync service
After=network.target
 
[Service]
Type=forking  #<==后台运行。
PIDFile=/var/run/rsyncd.pid                #<==PID路径。
ExecStart=/etc/rc.d/init.d/rsyncd start    #<==兼容CentOS6的启动服务脚本,介绍在下文。
ExecReload=/etc/rc.d/init.d/rsyncd restart #<==兼容CentOS6的重启服务脚本,介绍在下文。
ExecStop=/etc/rc.d/init.d/rsyncd stop      #<==兼容CentOS6的停止服务脚本,介绍在下文。
PrivateTmp=true    #<==开启独立的空间。
 
[Install]
WantedBy=multi-user.target  #<==这里为设置多用户级别。      

特别说明:本文仅仅使用rsync服务为例进行示例,对于系统没有的服务可以这样完成利用systemctl管理,但其实rsync服务系统已经早就配置好了,很简单简洁。

[root@oldboy ~]# cat /usr/lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf
 
[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"
 
[Install]
WantedBy=multi-user.target      
1.4 Centos6下的rsync专业启动脚本

下面是Centos6下的rsync专业启动脚本,在/usr/lib/systemd/system/rsyncd_oldboy.service里面会使用这个脚本。

[root@oldboy ~]# cat /etc/rc.d/init.d/rsyncd
#!/bin/bash
# chkconfig: 2345 21 81
# description: rsync service start and stop scripts
# Author: oldboy
# Organization: www.oldboyedu.com
[ -f /etc/rc.d/init.d/functions ] && source /etc/rc.d/init.d/functions
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/rsync"
rsyncd_pid_file_path="/var/run/rsyncd.pid"
#成功提示函数
log_success_msg(){
    #action为特殊的提示函数,$@为所有参数。
    action "SUCCESS! $@" /bin/true
}
#失败提示函数
log_failure_msg(){
    action  "ERROR! $@" /bin/false
}
start(){
    rsync --daemon &>/dev/null
    retval=$?
    if [ $retval -eq 0 ]
    then
        log_success_msg "Rsyncd is started."
        if test -w "$lockdir" #判断锁目录是否可写。
        then
            touch "$lock_file_path" #创建锁文件。
            return $retval
        else
            log_failure_msg "Rsync lockfile denied" #调用失败函数提示。
            return 1
        fi
    else
        echo "Rsyncd startup fail."
        return 1
    fi
}
stop(){
    if test -s "$rsyncd_pid_file_path"
    then
        #读取pidfile
        rsyncd_pid=`cat "$rsyncd_pid_file_path"`
      
        if (kill -0 $rsyncd_pid 2>/dev/null)
        then
            kill $rsyncd_pid
            retval=$?
            if [ $retval -eq 0 ]
            then
                log_success_msg "Rsync Stop" #调用停止成功函数。
                if test -f "$lock_file_path"
                then
                    rm "$lock_file_path"  #删除锁文件。
                fi
                return $retval
            else
                log_failure_msg "Rsyncd Stop."
                return $retval
            fi
        else
            log_failure_msg "rsync server_pid's process is not running!"
            rm "$rsyncd_pid_file_path"
        fi
    else
        log_failure_msg "Rsync server PID file is null or not exist!"
        return 1
    fi
}
case "$1" in
    start)
        start
        retval=$?
        ;;
    stop)
        stop
        retval=$?
        ;;
    restart)
        stop
        sleep 2
        start
        retval=$?
        ;;
    *)
        echo $"Usage:$0
        exit 1
esac
exit $retval      
1.5 配置开机自启动以及实现systemctl管理rsync服务

1、设置开机自启动

[root@oldboy ~]# systemctl enable rsyncd_oldboy.service      

2、查看设置状态

[root@oldboy ~]# systemctl status rsyncd_oldboy.service
rsyncd_oldboy.service - Rsync service
   Loaded: loaded (/usr/lib/systemd/system/rsyncd_oldboy.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
[root@oldboy ~]# systemctl is-enabled rsyncd_oldboy.service      

3、使用systemctl启动rsync并查看状态

[root@oldboy ~]# systemctl start rsyncd_oldboy.service
[root@oldboy ~]# systemctl status rsyncd_oldboy.service
● rsyncd_oldboy.service - Rsync service
   Loaded: loaded (/usr/lib/systemd/system/rsyncd_oldboy.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2018-08-07 18:56:06 CST; 23s ago
  Process: 1586 ExecStart=/etc/rc.d/init.d/rsyncd start (code=exited, status=0/SUCCESS)
 Main PID: 1590 (rsync)
   CGroup: /system.slice/rsyncd_oldboy.service
           └─1590 rsync --daemon
8月 07 18:56:06 oldboy systemd[1]: Starting Rsync service...
8月 07 18:56:06 oldboy rsyncd[1590]: rsyncd version 3.1.2 starting, listening on port 873
8月 07 18:56:06 oldboy rsyncd[1586]: SUCCESS! Rsyncd is started. [  确定  ]
8月 07 18:56:06 oldboy systemd[1]: Started Rsync service.      

4、使用lsof检查真实服务状态

[root@oldboy ~]# lsof -i :873 #<==需要yum install lsof -y。
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   1590 root    4u  IPv4  25765      0t0  TCP *:rsync (LISTEN)
rsync   1590 root    5u  IPv6  25766      0t0  TCP *:rsync (LISTEN)      

5、使用systemctl停止rsync并查看状态

[root@oldboy ~]# systemctl stop rsyncd_oldboy.service
[root@oldboy ~]# lsof -i :873      
[root@oldboy ~]# systemctl disable rsyncd_oldboy
Removed symlink /etc/systemd/system/multi-user.target.wants/rsyncd_oldboy.service.
[root@oldboy ~]# systemctl is-enabled rsyncd      

继续阅读