導讀
Linux 的交互式 Shell 與 Shell 腳本存在一定的差異,主要是由於後者存在一個獨立的運行進程,因此在獲取進程 pid 上二者也有所區別。
交互式 Bash Shell 獲取進程 pid
在已知進程名(name)的前提下,交互式 Shell 獲取進程 pid 有很多種方法,典型的通過 grep 獲取 pid 的方法為(這裡添加 -v grep是為瞭避免匹配到 grep 進程):
ps -ef | grep "name" | grep -v grep | awk '{print $2}'
或者不使用 grep(這裡名稱首字母加[]的目的是為瞭避免匹配到 awk 自身的進程):
ps -ef | awk '/[n]ame/{print $2}'
如果隻使用 x 參數的話則 pid 應該位於第一位:
ps x | awk '/[n]ame/{print $1}'
最簡單的方法是使用 pgrep:
pgrep -f name
如果需要查找到 pid 之後 kill 掉該進程,還可以使用 pkill:
pkill -f name
如果是可執行程式的話,可以直接使用 pidof
pidof name
Bash Shell 腳本獲取進程 pid
根據進程名獲取進程 pid
在使用 Shell 腳本獲取進程 pid 時,如果直接使用上述命令,會出現多個 pid 結果,例如
#! /bin/bash # process-monitor.sh process=$1 pid=$(ps x | grep $process | grep -v grep | awk '{print $1}') echo $pid
執行 process-monitor.sh 會出現多個結果:
$> sh process-monitor.sh 3036 3098 3099
進一步排查可以發現,多出來的幾個進程實際上是子 Shell 的(臨時)進程:
root 3036 2905 0 09:03 pts/1 00:00:45 /usr/java/jdk1.7.0_71/bin/java ...name root 4522 2905 0 16:12 pts/1 00:00:00 sh process-monitor.sh name root 4523 4522 0 16:12 pts/1 00:00:00 sh process-monitor.sh name
其中 3036 是需要查找的進程pid,而 4522、4523 就是子 Shell 的 pid。 為瞭避免這種情況,需要進一步明確查找條件,考慮到所要查找的是 Java 程式,就可以通過 Java 的關鍵字進行匹配:
#! /bin/bash # process-monitor.sh process=$1 pid=$(ps -ef | grep $process | grep '/bin/java' | grep -v grep | awk '{print $2}') echo $pid
獲取 Shell 腳本自身進程 pid
這裡涉及兩個指令:
1. $$ :當前 Shell 進程的 pid
2. 2. $! :上一個後臺進程的 pid 可以使用這兩個指令來獲取相應的進程 pid。例如,如果需要獲取某個正在執行的進程的 pid(並寫入指定的档案):
myCommand && pid=$! myCommand & echo $! >/path/to/pid.file
註意,在腳本中執行 $! 隻會顯示子 Shell 的後臺進程 pid,如果子 Shell 先前沒有啟動後臺進程,則沒有輸出。
查看指定進程是否存在
在獲取到 pid 之後,還可以根據 pid 查看對應的進程是否存在(運行),這個方法也可以用於 kill 指定的進程。
if ps -p $PID > /dev/null then echo "$PID is running" # Do something knowing the pid exists, i.e. the process with $PID is running fi
到此這篇關於linux shell 根據進程名獲取pid的實現方法的文章就介紹到這瞭,更多相關shell 進程名獲取pid內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支援!