systemd

1.Journalctl查看並操作Systemd日誌

  1. 被隱藏的部分日誌可以通過左右箭頭按鍵查看, 如果不想分頁輸出,那麼可以使用 --no-pager 選項
  2. 常用選項
    journalctl -a 顯示全部信息,無論其是否包含不可輸出的字符
    journalctl -b本次啓動後的所有日誌
    journalctl -b -1查看上次啓動後的日誌(需要在配置文件啓用持久記錄)
    journalctl -u 查看指定服務日誌信息(systemd的單元)
    journalctl --since和--until查看指定時間的日誌
    journalctl -f動態顯示日誌
    journalctl -p按日誌警告級別查看

    示例:
    journalctl --since "2017-01-10" --until "2017-01-11 03:00"
    journalctl --since "20 min ago"
    journalctl -u httpd.service --since today
    journalctl -p err -b

  3. 修改成持久記錄
    vim    /etc/systemd/journald.conf
    . . .
    [Journal]
    Storage=persistent
  4. 日誌警告級別
    0: emerg
    1: alert
    2: crit
    3: err
    4: warning
    5: notice
    6: info
    7: debug

2. 在centos7中手動設置Nginx開機啓動

1.網上下載nginx安裝包並解壓
tar xvf nginx-1.14.2.tar.gz
2.編譯nginx
./configure --prefix=/data/nginx1.4
3.安裝nginx
make -j 4 && make install
4.啓動測試nginx

[root@centos7 ~]#/data/nginx1.4/sbin/nginx -h
nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /data/nginx1.4/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file
/data/nginx1.4/sbin/nginx 
[root@centos7 nginx-1.14.2]#curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

5.在/etc/systemd/system文件夾新建service文件讓systemd管理

[root@centos7 ~]#vim nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/data/nginx1.4/sbin/nginx
ExecReload=/data/nginx1.4/sbin/nginx -s reload
ExecStop=/data/nginx1.4/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

如果不是默認配置文件路徑,在service中加入EnvironmentFile 路徑
如:EnvironmentFile=/data/nginx1.4/conf/nginx.conf
6.讓systemd加載該service
systemctl daemon-reload
7.設置開機啓動

[root@centos7 system]#systemctl enable nginx.service        
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /etc/systemd/system/nginx.service.

8.重啓系統測試

[root@centos7 ~]#systemctl status nginx.service                                    
● nginx.service - nginx
   Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-01-03 19:34:00 CST; 50s ago
  Process: 960 ExecStart=/data/nginx1.4/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 973 (nginx)
   CGroup: /system.slice/nginx.service
           ├─973 nginx: master process /data/nginx1.4/sbin/nginx
           └─975 nginx: worker process

Jan 03 19:34:00 centos7.dcrfan systemd[1]: Starting nginx...
Jan 03 19:34:00 centos7.dcrfan systemd[1]: Started nginx.

3. SIGHUP、SIGQUIT、 SIGTERM、 SIGINT區別

1) SIGHUP: 無須關閉進程而讓其重讀配置文件
2) SIGINT: 中止正在運行的進程;相當於Ctrl+c ,用於通知前臺進程組終止進程
3) SIGQUIT:相當於ctrl+\ ,進程在因收到SIGQUIT退出時會產生core文件, 在這個意義上類似於一個程序錯誤信號。
15) SIGTERM:終止正在運行的進程 ,通常用來要求程序自己正常退出,shell命令kill缺省產生這個信號。如果進程終止不了,我們纔會嘗試SIGKILL(9)。


4.利用awk實現tcp連接中處於listen個數統計

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

或者

[root@centos7 ~]#netstat -tan | awk '$NF=="LISTEN"{state[$NF]++}END{for (i in state) {print i,state[i]}}'
LISTEN 12
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章