linux的functions之__pids_var_run函數詳解

# 該函數有二個做用:
 # 一、根據給定的二進制運行程序,找出該程序的進程號(PID)。# 二、判斷該二進制程序的運行狀態,輸出不一樣的返回値,供查看一個程序的運行狀態時使用。# __proc_pids {program} [pidfile]   -----> 該函數的用法# Set $pid to pids from /var/run* for {program}.# $pid should be declared# local in the caller.# Returns LSB exit code for the 'status' action.__pids_var_run() {
        local base=${1##*/}  
        # 參數替換,使用局部變量$base 存儲程序的二進制文件名。
        local pid_file=${2:-/var/run/$base.pid}  
         # 參數替換。若是調用該函數時,沒有給$2或爲空,將使# 用/var/run/$base.pid做爲變量 pid_file的值。
        pid=       # 設置一個全局變量,但其值爲空。記錄進程的PID號
        if [ -f "$pid_file" ] ; then   # 判斷變量的值是不是一個文件,
                local line p                read line < "$pid_file"  # 讀取文件的內容
                for p in $line ; do  
                    [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" 
              # 找出二進制運行程序的PID號。
              # 一個二進制可運行程序啓動後,通常會在/var/run/目錄下建立一個   
              # $base.pid 文件。
      #同時會把本身的進程號(PID)寫到該文件中,固然有可能也會把另外的信息寫到該文件中
      # 同時系統會在/proc 目錄中建立一個以該二進制運行程序的PID爲名稱的目錄
      # 該目錄保存了,二進制運行程序的使用資源狀況等等。
      #也有例外:/var/run/$base.pid 文件是空的。也有程序把pid文件存放在其它目
      #的。mysql
                done
                if [ -n "$pid" ]; then  
                        return 0        
                        # 若是找到進程號就返回狀態碼 0.表明二進制程序是運行的。
                fi
                return 1 # "Program is dead and /var/run pid file exists"   
         # 若是找不到進程號,就返回狀態碼 1.可是: /var/run/目錄下是有pid文件的。
         # $base.pid是空的,也會找不到PID的。可是並不表明$base是不運行的。
        fi
        return 3 # "Program is not running"  
     # 若是:/var/run/ 目錄上沒有pid文件,就返回狀態碼 3 .該二進制程序是中止的。}
相關文章
相關標籤/搜索