Linux系统管理之systemd、awk

2019-01-30  本文已影响0人  请叫我飘哥

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

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

root@node01 ~]# cd /usr/lib/systemd/system 
[root@node01 system]# vim nginx.service
[Unit]
Description=nginx web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/bin/rm -f $PIDFile
ExecStartPre=/usr/local/nginx/nginx  -t
ExecStart=/usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf
ExecReload=/usr/local/nginx/nginx -s reload
ExecStop=/usr/local/nginx/nginx -s stop
[Install]
WantedBy=multi-user.target
[root@kernel_update system]# systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@kernel_update system]# systemctl start nginx.service 
[root@kernel_update system]# ss -tan
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:80                                                                     *:*                  
LISTEN     0      10                                              10.192.2.221:53                                                                     *:*                  
LISTEN     0      10                                                 127.0.0.1:53                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                127.0.0.1:953                                                                    *:*                  
LISTEN     0      100                                                127.0.0.1:25                                                                     *:*                  
ESTAB      0      52                                              10.192.2.221:22                                                          10.192.52.63:56916              
ESTAB      0      0                                               10.192.2.221:22                                                          10.192.52.63:58931              
LISTEN     0      10                                                       ::1:53                                                                    :::*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
LISTEN     0      128                                                      ::1:953                                                                   :::*                  
LISTEN     0      100                                                      ::1:25                                                                    :::*                  
[root@kernel_update system]# systemctl status nginx
● nginx.service - nginx web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-01-30 16:49:45 CST; 23s ago
  Process: 1708 ExecStart=/usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 1704 ExecStartPre=/usr/local/nginx/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1700 ExecStartPre=/usr/bin/rm -f $PIDFile (code=exited, status=0/SUCCESS)
 Main PID: 1710 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1710 nginx: master process /usr/local/nginx/nginx -c /usr/local/nginx/nginx.conf
           └─1711 nginx: worker process

1月 30 16:49:45 kernel_update systemd[1]: Starting nginx web server...
1月 30 16:49:45 kernel_update nginx[1704]: nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
1月 30 16:49:45 kernel_update nginx[1704]: nginx: configuration file /usr/local/nginx/nginx.conf test is successful
1月 30 16:49:45 kernel_update systemd[1]: Started nginx web server.

[root@node01 system]# systemctl  daemon-reload

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

信号(signal)是Linux进程间通信的一种机制,全称为软中断信号,也被称为软中断。信号本质上是在软件层次上对硬件中断机制的一种模拟。
linux操作系统支持的信号种类。我们使用kill -l 命令查看,如下

[root@node01 ~]# kill -l
 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP
 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR
31) SIGSYS  34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX    
[root@node01 ~]# 

常见信号说明:

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

思路:
1、ss -tan 显示只有tcp连接的状态

[root@node01 ~]# ss -tan
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      10                                              10.192.2.222:53                                                                     *:*                  
LISTEN     0      10                                                 127.0.0.1:53                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                127.0.0.1:953                                                                    *:*                  
LISTEN     0      100                                                127.0.0.1:25                                                                     *:*                  
ESTAB      0      52                                              10.192.2.222:22                                                          10.192.52.63:53269              
LISTEN     0      128                                                       :::80                                                                    :::*                  
SYN-RECV   0      0                         ::1000:0:ccc2:c7b8:0:0%if360615604:80                                                 ::1986:cbdf:1986:cbdf:57310              
SYN-RECV   0      0                                      0:0:200::%if360615604:80                                                 ::b2d9:c3fe:b3d9:c3fe:57309              
LISTEN     0      10                                                       ::1:53                                                                    :::*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
LISTEN     0      128                                                      ::1:953                                                                   :::*                  
LISTEN     0      100                                                      ::1:25                                                                    :::*                  
ESTAB      0      0                                        ::ffff:10.192.2.222:80                                                   ::ffff:10.192.52.63:57298              
ESTAB      0      0                                        ::ffff:10.192.2.222:80                                                   ::ffff:10.192.52.63:57306              
ESTAB      0      0                                        ::ffff:10.192.2.222:80                                                   ::ffff:10.192.52.63:57308              
ESTAB      0      0                                        ::ffff:10.192.2.222:80                                                   ::ffff:10.192.52.63:57307              
TIME-WAIT  0      0                                        ::ffff:10.192.2.222:80                                                   ::ffff:10.192.52.63:57293              
TIME-WAIT  0      0                                        ::ffff:10.192.2.222:80                                                   ::ffff:10.192.52.63:57294              
[root@node01 ~]# 

2、我们看到状态信息在第一列。我们在利用awk 数组的特性如果某数组元素事先不存在,在引用时,awk会自动创建此元素,并将其值初始化为“空串”我们在计算数组中的元素个数。

[root@node01 ~]# ss -tan |awk '{state[$1]++}END {for(i in state)print i, state[i]}'
SYN-RECV 2
LISTEN 10
ESTAB 5
State 1
TIME-WAIT 4
[root@node01 ~]# 

3、这里已经得到了所有我们想要的状态信息。这里我们再用grep过滤一下即可,也可以直接使用awk进行过滤

[root@node01 ~]# ss -tan |awk '{state[$1]++}END {for(i in state)print i, state[i]}' |grep "LISTEN"
LISTEN 10
[root@node01 ~]# ss -tan |awk '/LISTEN/ {state[$1]++}END {for(i in state)print i, state[i]}' 
LISTEN 10

上一篇下一篇

猜你喜欢

热点阅读