# Linux 2020 讀書會 — 第六次 [toc] ## Subject 1 - 程序管理與 SELinux ### job control - 前景 (foreground) - 可控與下指令 - `fg` + `%<process_id>` : 將背景執行程式叫回前景執行 - `jobs -l`: 顯示目前 bash 所有的工作 - 背景 (background) - 不能與 user 互動 - 不可使用 ctrl+c 終止 - 背景中『執行』的程序不能等待 terminal/shell 的輸入(input) - 會輸出 process id - +`&`: 在後台開始執行一個 process - `bg`: 將程式丟到背景執行 - `CTRL+Z`: 將前台任務丟到後台中暫停 - Process Status (ps) - F: 0 = User 本身, F: 4 = Root - top - `top -d 2 -p $(echo $$)` - `-d 2` 每兩秒更新一次 - `-p {something}` 指定Pid - `$$` 當前的 Bash ID - renice - htop - 加強版top - 支援滑鼠操作 - 樹狀 Process - 顏色標記 Process 狀態 - Glances - 開放原始碼 - pstree - 樹狀圖顯示 Process 關係 - demo - `pstree -up`: - `pstree [user]`: user 的使用量 - kill (give a signal) - `kill <signal_number>` / `kill <signal_name>` - 代號 - 1: SIGHUP(start and reload) - 2: SIGINT(ctrl + c) - 9: SIGKILL(Force stop) - 15: SIGTERM(Stop on a formal porcedure) - 19: SIGSTOP(ctrl + z) > kill -9 滿常好的,程式死了關不掉的萬靈丹 <br> > 補充一下:nohup 就是避免該程式接收到 SIGHUP 訊號 > [name=JackKuo] - Process Priority - PRI 越低越優先(無法調整) + Nice (-20~19) - Root 可以調整 Nice, User 只能調整自己的 Nice (只能上升) - - CPUlimit - `sudo apt-get install cpulimit` - `cpulimit --pid 21203 --limit 50` - free 觀察記憶體使用情況 - free -m > swap 盡量不要超過 memory 的 20%,不然系統會變慢 - uname 查閱系統與核心相關資訊 - uptime 觀察系統啟動時間與工作負載 - netstat 追蹤網路或插槽檔 - `sudo netstat -antulp | grep 80` - dmesg: 分析核心產生的訊息 - 分析核心產生的訊息 - 看有網路有沒有抓到 - 硬碟有沒有 mount - vmstat: 偵測系統資源變化 - `vmstat 1 3`: 每一秒 Print 一次,總共 Print 3 次 ## 程序管理 與 SELinux ### 程序管理 - `/proc/` - 程序都是在記憶體中 - 記憶體當中的資料又都是寫入到 /proc/* 這個目錄下 - 不是一個真正的檔案系統,也就不占用儲存空間,只是占用有限的記憶體 - `fuser`: 看哪個 Process 佔用該檔案/ Socket / FileSystem - `lsof`: List of open file - 查看網路使用可以用 `netstat` , 也可以用這個。 ### SELinux - 設計目標:避免資源的誤用 - 傳統的檔案權限與帳號關係:自主式存取控制, DAC - Discretionary Access Control, DAC - root 無所不能 - 以政策規則訂定特定程序讀取特定檔案:委任式存取控制, MAC - Mandatory Access Control, MAC - 控制的『主體』變成了『程序』而不是使用者 :::info [JackKuo 分享] 工具 - pcm C - 手刻 netstat / 從 /proc 中讀出來的資訊 https://github.com/JackKuo-tw/Courses/tree/master/unix_programming/hw/hw1 Session / fg / bg process 關係 https://www.notion.so/jiffkuo/Process-Process-Group-Session-075cab63ade04dc0b035553bfb638684 - Process Group 參考資訊 [Comment by Jerry Wang] https://en.wikipedia.org/wiki/Process_group 簡易產生 Zombie https://www.notion.so/jiffkuo/Process-Control-9a28441e17b44aa3b80f3fa947ff05d2 Unix Programming (交大) https://people.cs.nctu.edu.tw/~chuang/courses/unixprog/ With JackKuo 筆記 https://www.notion.so/jiffkuo/Unix-Programming-191bfb7a96a541d0a45f3b885bc02c2b [Jerry Wang 分享] 終端工具 - tmux/screen + byobu - 不用另外開 Session,在同一個 Session 中用分頁的方式快速使用 Process 管理參考文章 https://netflixtechblog.com/linux-performance-analysis-in-60-000-milliseconds-accc10403c55 http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html 工具 sar - 封包、CPU、Memory 等狀態 - 以 30 天為一個 rotate, default 600 秒記錄一次 - `sar -f sa日期`: 顯示該天的系統狀態 atop - 每天去紀錄, default 600 秒記錄一次 :::