美文网首页
week_8_systemd、signal、awk

week_8_systemd、signal、awk

作者: 人間失格_430b | 来源:发表于2019-03-22 19:59 被阅读0次

    Q:

    1、systemd查看日志文件有隐藏该如何处理?
    2、自己动手写一个systemd的配置文件, 让nginx服务可以开机启动
    3、SIGHUP、SIGQUIT、SIGTERM、SIGINTERRUPT的区别
    4、用awk查看tcp连接处于TIMEOUT的连接个数


    A:

    1、systemd查看日志文件有隐藏该如何处理?

    CentOS 7日志系统由 systemd-journald 和 rsyslog 两个服务组成默认,systemd日志保存于/run/log/journal中,系统重启后会清除,rsyslog会根据一定的规则,将日志写到到/var/log目录中永久保存systemd统一管理所有Unit的启动日志,包含内核日志和应用日志。

    查看单个服务的日志

    [root@localhost ~]# systemctl status nginxTest -l
    ● nginxTest.service - This is a nginx test service
       Loaded: loaded (/etc/systemd/system/nginxTest.service; enabled; vendor preset: disabled)
       Active: active (running) since Fri 2019-03-22 02:23:49 EDT; 10min ago
      Process: 8570 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
      Process: 8554 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
      Process: 8542 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
     Main PID: 8572 (nginx)
       CGroup: /system.slice/nginxTest.service
               ├─8572 nginx: master process /usr/sbin/ngin
               ├─8573 nginx: worker proces
               ├─8574 nginx: worker proces
               ├─8575 nginx: worker proces
               └─8577 nginx: worker proces
    
    Mar 22 02:23:49 localhost.localdomain systemd[1]: Starting This is a nginx test service...
    Mar 22 02:23:49 localhost.localdomain nginx[8554]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Mar 22 02:23:49 localhost.localdomain nginx[8554]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Mar 22 02:23:49 localhost.localdomain systemd[1]: Started This is a nginx test service.
    [root@localhost ~]# journalctl -u nginxTest
    -- Logs begin at Thu 2019-03-21 23:18:05 EDT, end at Fri 2019-03-22 02:34:14 EDT. --
    Mar 22 02:17:09 localhost.localdomain systemd[1]: [/etc/systemd/system/nginxTest.service:7] Unknown lvalue 'PIDfile' in section 'Service'
    Mar 22 02:17:09 localhost.localdomain systemd[1]: [/etc/systemd/system/nginxTest.service:7] Unknown lvalue 'PIDfile' in section 'Service'
    Mar 22 02:22:39 localhost.localdomain systemd[1]: [/etc/systemd/system/nginxTest.service:7] Unknown lvalue 'PIDfile' in section 'Service'
    Mar 22 02:23:49 localhost.localdomain systemd[1]: Starting This is a nginx test service...
    Mar 22 02:23:49 localhost.localdomain nginx[8554]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Mar 22 02:23:49 localhost.localdomain nginx[8554]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Mar 22 02:23:49 localhost.localdomain systemd[1]: Started This is a nginx test service.
    

    尴尬的显示:在我手动添加的unit文件中PIDfile字段无法解析,应该是PIDFile

    systemd的日志文件直接cat是乱码(who fucking knows how to check this file)

    [root@localhost ~]# ls /run/log/journal/5f47125a1faf4b438a9b7aec3758819e/system.journal -l
    -rwxr-x---+ 1 root systemd-journal 6438912 Mar 22 02:25 /run/log/journal/5f47125a1faf4b438a9b7aec3758819e/system.journal
    

    直接使用journalctl查看日志

    [root@localhost ~]# journalctl  -n 10 
    -- Logs begin at Thu 2019-03-21 23:18:05 EDT, end at Fri 2019-03-22 02:34:14 EDT. --
    Mar 22 02:34:14 localhost.localdomain NetworkManager[5877]: <info>  [1553236454.8042] dhcp4 (ens33):   nameserver '192.168.223.2'
    Mar 22 02:34:14 localhost.localdomain NetworkManager[5877]: <info>  [1553236454.8042] dhcp4 (ens33):   domain name 'localdomain'
    Mar 22 02:34:14 localhost.localdomain NetworkManager[5877]: <info>  [1553236454.8043] dhcp4 (ens33): state changed bound -> bound
    Mar 22 02:34:14 localhost.localdomain dbus[5788]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-
    Mar 22 02:34:14 localhost.localdomain dhclient[6174]: bound to 192.168.223.131 -- renewal in 843 seconds.
    Mar 22 02:34:14 localhost.localdomain systemd[1]: Starting Network Manager Script Dispatcher Service...
    Mar 22 02:34:14 localhost.localdomain dbus[5788]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
    Mar 22 02:34:14 localhost.localdomain systemd[1]: Started Network Manager Script Dispatcher Service.
    Mar 22 02:34:14 localhost.localdomain nm-dispatcher[8769]: req:1 'dhcp4-change' [ens33]: new request (3 scripts)
    Mar 22 02:34:14 localhost.localdomain nm-dispatcher[8769]: req:1 'dhcp4-change' [ens33]: start running ordered scripts...
    

    2、自己动手写一个systemd的配置文件, 让nginx服务可以开机启动

           Table 1.  Load path when running in system mode (--system).
           ┌────────────────────────┬─────────────────────────────┐
           │Path                    │ Description                 │
           ├────────────────────────┼─────────────────────────────┤
           │/etc/systemd/system     │ Local configuration         │
           ├────────────────────────┼─────────────────────────────┤
           │/run/systemd/system     │ Runtime units               │
           ├────────────────────────┼─────────────────────────────┤
           │/usr/lib/systemd/system │ Units of installed packages │
           └────────────────────────┴─────────────────────────────┘
    

    通过查看nginx状态获知nginx.service unit文件路径

    [root@localhost ~]# systemctl status nginx
    ● nginx.service - The nginx HTTP and reverse proxy server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
       Active: inactive (dead)
    
    Mar 22 00:20:22 localhost.localdomain systemd[1]: Unit nginx.service cannot be reloaded because it is inactive.
    
    [root@localhost ~]# cat /usr/lib/systemd/system/nginx.service 
    [Unit]
    Description=The nginx HTTP and reverse proxy server
    After=network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    # Nginx will fail to start if /run/nginx.pid already exists but has the wrong
    # SELinux context. This might happen when running `nginx -t` from the cmdline.
    # https://bugzilla.redhat.com/show_bug.cgi?id=1268621
    ExecStartPre=/usr/bin/rm -f /run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t
    ExecStart=/usr/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    手动写一个unit文件并enable

    [root@localhost ~]# vim /etc/systemd/system/nginxTest.service
    [root@localhost ~]# systemctl enable nginxTest
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginxTest.service to /etc/systemd/system/nginxTest.service.
    [root@localhost ~]# systemctl daemon-reload
    [root@localhost ~]# reboot
    
    [root@localhost ~]# systemctl get-default
    multi-user.target
    

    由于在unit文件Install段内注明此服务被多用户目标所弱依赖,所以在开机启动后默认进入多用户模式并启动nginxTest服务

    不重启手动启动多用户模式查看nginxTest服务状态

    [root@localhost ~]# systemctl status nginxTest
    ● nginxTest.service - This is a nginx test service
       Loaded: loaded (/etc/systemd/system/nginxTest.service; enabled; vendor preset: disabled)
       Active: inactive (dead)
    
    Mar 22 02:17:09 localhost.localdomain systemd[1]: [/etc/systemd/system/nginxTest.service:7] Unknown lvalue 'PIDfile' in section 'Service'
    Mar 22 02:17:09 localhost.localdomain systemd[1]: [/etc/systemd/system/nginxTest.service:7] Unknown lvalue 'PIDfile' in section 'Service'
    Mar 22 02:22:39 localhost.localdomain systemd[1]: [/etc/systemd/system/nginxTest.service:7] Unknown lvalue 'PIDfile' in section 'Service'
    [root@localhost ~]# systemctl isolate multi-user.target
    [root@localhost ~]# systemctl status nginxTest
    ● nginxTest.service - This is a nginx test service
       Loaded: loaded (/etc/systemd/system/nginxTest.service; enabled; vendor preset: disabled)
       Active: active (running) since Fri 2019-03-22 02:23:49 EDT; 6s ago
      Process: 8570 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
      Process: 8554 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
      Process: 8542 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
     Main PID: 8572 (nginx)
       CGroup: /system.slice/nginxTest.service
               ├─8572 nginx: master process /usr/sbin/nginx
               ├─8573 nginx: worker process
               ├─8574 nginx: worker process
               ├─8575 nginx: worker process
               └─8577 nginx: worker process
    
    Mar 22 02:23:49 localhost.localdomain systemd[1]: Starting This is a nginx test service...
    Mar 22 02:23:49 localhost.localdomain nginx[8554]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Mar 22 02:23:49 localhost.localdomain nginx[8554]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Mar 22 02:23:49 localhost.localdomain systemd[1]: Started This is a nginx test service.
    

    3、SIGHUP、SIGQUIT、SIGTERM、SIGINTERRUPT的区别

    没有signal的manual文件提示及解决方法

    [root@localhost ~]# man signal
    No manual entry for signal
    
    [root@localhost ~]# yum install man-pages
    
           Signal     Value     Action   Comment
           ──────────────────────────────────────────────────────────────────────
           SIGHUP        1       Term    Hangup detected on controlling terminal
                                         or death of controlling process
           SIGINT        2       Term    Interrupt from keyboard
           SIGQUIT       3       Core    Quit from keyboard
    
           SIGTERM      15       Term    Termination signal
    

    4、用awk查看tcp连接处于TIMEOUT的连接个数

    [root@localhost ~]# netstat -tan |awk '{$NF~/^TIMEOUT$/?count++:Continue}END{printf "The number of links ,which has TIMEOUT stat : %d\n",count}'
    The number of links ,which has TIMEOUT stat : 0
    

    相关文章

      网友评论

          本文标题:week_8_systemd、signal、awk

          本文链接:https://www.haomeiwen.com/subject/ikdlvqtx.html