作者:芨芨草_的美丽家园 | 来源:互联网 | 2023-08-28 15:06
本文由编程笔记#小编为大家整理,主要介绍了markdown Linux类的注释相关的知识,希望对你有一定的参考价值。
# Introduction to Linux process and management
> foreground vs. backgraound process(job)
- `ctrl+z`: switch to background but stop
- `&`: switch to background and execute
- switch to foreground: `fg`
- check for job list: `jobs`
- `bg %1`: execute the first program which is stopped in the background
> nohup
>
- `nohup [job] &`: you can logout after doing nohup command, and next time when you login again you can see the result of the job in nohup.out this file
> process management `-ps`
- `ps`: show all the jobs which the user are executing currently
- `-a`: show all the process stat which has 終端控制介面
- `-A`, `-e`: show all the process stat in the system
- `x`: show 不具備終端控制介面的process information
- `-w`: show the 詳細information of the process
- `u`: use username and boot time 順序to show the process information
- `f`: show the realtionship of process use 樹狀
- `-f`: 完整格式顯示
- `-l`: 以長資訊格式顯示
- `-U`: show 實際user UID
- `-u`: show effective user UID
- `-C command`: show 符合command 指令的process
- `ps u -u username`: using BSD style to show information
- `watch -n [sec] 'ps [參數]`: 讓ps result keep updating frequency [sec] seconds
> process management `-pstree`
- `pstree`: get all the relationship of all the process in system
- `-p`: show all the PID in execution
- `-u`: show the UID of the process
> top command
- `top`: show the process's resource佔用狀態
- `top -d 1`: set the updating frequency is 1 second, default is 3 sec
- while executing top command the cammand you can use
- `h`: show 操作說明 information
- `r`: can change the NI of process in execution
- `k`: 傳遞控制訊號to a process in execution
- `N`: sort using PID
- `P`: sort using CPU 使用率
- `i`: only show the process which is not in waiting stat
- `top -u username`: 監控user's process stat
- `top -p pid`: trace specific process stat
- `top -b -n 1 > top.1`: 進行批次運作模式,且執行一次,導到top.1這個檔案
> process priority `nice`
- `nice`: search for the default NI setting of user
- `nice -n +10 [process]`: give the process default NI 10
- `renice [num] pid`: 調整the executing priority of process in execution (NI)
- PRInew=PRIold+(NInew-NIold)
> process lifecycle `-kill`
- `kill`: terminate the process in execution
- `kill -l`: list all the signal list that can be used currently
- `kill -s`: 送出specific signal to process, you can use signal name or number
- `pidof [process]`: search the PID of the process
__common process termination signal__
- 1 `SIGUP`: 與terminal所建立的process, inform the process that the terminal is closing. if it's a Daeman process than 重置execution environment
- 2 `SIGINT`: through keyboard execute interruption, `ctrl-c`
- 9 `SIGKILL`: through kernel terminate the process immediately
- 15 `SIGTERM`: 請求process to terminate by itself
- 18 `SIGCONT`: continue the execution of a stop process
- 19 `STOP`: pause the process
- `kill -9 PID`, `kill -SIGKILL PID`, `kill -s SIGKILL`: terminate the process immediately
- `kill -signal [jobid]`: you can also send signal to jobs
- `killall -signal [process]`: you don't need to know the PID and can still send signal to process
> process lifecycle `pkill`
- `pkill -signal [pattern]`: the function if pkill is same as kill, but it can 支援regular expression in [pattern]
- `-f`: [pattern]所描述的指令列資訊為完整格式,including command, 參數與引述的information
- `-x`: completely 符合[pattern]所描述的process than send signal
- `-t [terminal]`: send signal to all the process which is connect with [terminal]
- `-u UID`: send signal to the process which has userid UID
- `-P PPID`: send signal to the process which has ppid PPID
`xkill`: can use in graphical window
# shell script
> how to execute shell script
- method 1
1. 建立建立一個shell file: `~/shell.sh`
2. `chmod u+rx`
3. 使用絕對路徑或相對路徑或shell所在目錄所在目錄放到$PATH環境變數中
4. `./shell.sh`
- method 2: use bash command to execute
1. `bash shell.sh`, `sh shell.sh`, `source shell.sh`
2. at this time the file only need to have the right of being execute
> basic syntax for shell script
- `#!/bin/bash`: 宣告this file is using bash syntax
- `if`, `then`,`case`, `esac`:條件判斷
- loop: `while do done`, `until do done`,`for`, `do`, `done`
> test command(檢測系統上檢測系統上某些檔案檢測系統上某些檔案或者是相關的屬性)
- `test -e [file] && echo "exist" || echo "not exist"`: if the file exist then it will show exist, if not it will show not exist
__file type判斷__
- `-e`: if the filename exist
- `-f`: if the filename exist and also is a regular file
- `-e`: if it is a directory
- `-b`: if it is a block device
- `-c`: if it is a character device
- `-S`: if it is a Socket file
- `-p`: if it is a FIFO(pipe) file
- `-L`: if it is a link file
__file 權限偵測__
- `-r`: if it has可讀權限
- `-w`: if it has可寫權限
- `-x`: if it has 可execute權限
- `-u`: if it has SUID屬性
- `-g`: if it has SGID屬性
- `-k`: if it has sticky bit屬性
- `-s`: if it isn't a blank file
__two files comparison__
- `test file1 -nt file2`: 判斷if file1 is newer than file2
- `-ot`:判斷if file1 is older than file2
- `ef`: 判斷if file1 and file2 are the same file, can be used to check hard link--> if the two files are pointing to the same inode
__check to integers__
- `test n1 -eq n2`: if n1 and n2 are equal
- `-ne`: not equal
- `-gt`: greater than
- `-lt`: less than
- `-ge`: greater than or equal
- `-le`: less than or equal
__check string information__
- `test -z string`: if the string is blank than return true
- `test -n string`: if the string is blank than return false, `-n` can be avoid
- `test str1==str2`: if str1 equal str2, if equal than return true
- `test str1!=str2`: if str1 isn't equal to str2, if equal than return false
__multiple 條件check__
- `-a`: and two condition both 成立,`test -r file -a -x file`: if file has both r and x權限,than return true
- `-o`: or two condition 任何一個成立,`test -r file -o -x file`: if the file has r or x權限, than return true
- `!`: 反向選擇,反向選擇,`test ! -x file`: if file doesn't has x權限,than return true
> variables in shell script
- `$0`: the name af this shell script
- `$1`,`$2`,,,: the first variable, the second variable,,,
- `$#`: 代表後面接的參數個數
- `"$@"`: represent "$1" "$2" ,,,
- `"$*"`: represent "$1c$2c,,," --> c為separate character, default is spacebar
> function
- define a function
```shell
function fname{
code
}
```
- variable代號: function name --> `$0`, 後續的variable --> `$1`,`$2`,,,,
- 回傳值: return
> if-then-else
- single layer simple判斷
```shell
if [condition判斷式]; then
# if the condition 成立,then you can execute the job
fi
```
- multiple layer complicated判斷式
```shell
if[condition 1]; then
# if 成立 execute
elif [condition 2]; then
# if 成立 execute
else
fi
```
> case esac
```shell
case ${variable} in
"first variable content")
#code
;;
"second variable content")
code
;;
*) # last variable content
#code
;;
esac
```
> while loop
- while-do-done
```shell
while [condition] # condition就是就是條件判斷式
do # loop start
#code
done
```
- until-do-done
```shell
until [condition]
do
#code
done
```
> for loop
```shell
for variable in condition
do
#code
done
```
```shell
for variable in $(seq range1 range2)
```
- `seq`: abbreviation of sequence, 連續的意思,from range1 to range2連續
```shell
for((初始值;限制值;執行步階))
do
# code
done
```
> debug
- `sh [參數] script.sh`
- `-n`: do not execute the script, only search for syntax error
- `-v`: show the script's content on the screen before executing the script
- `-x`: show the script's content which is in use while executing on the screen