2014-02-10 30 views
1

你好,我創建了一個腳本殺按年齡排序但是每次PID被改變的過程...我怎樣才能解決這個 這裏是我的腳本PID更改每次我嘗試要殺死他們

#!/bin/bash 
    #Argument = -c check -k kill -l list 
usage() 
{ 
cat << EOF 
usage: $0 options 

This script kills all the processes running and leaves the last one sorted by age running. 

OPTIONS: 
    -c  checks how many proccess are runnig it needs string argument 
    -k  Kill all the processes and leaves just the last sorted by age running 
    -l  Show the list of procesess to be killed. 
EOF 
} 

CHECK= 
KILL= 
LIST= 

while getopts "hc:k:l:" OPTION 
do 
    case $OPTION in 
     h) 
      usage 
      exit 1 
      ;; 
     c) 
      CHECK=$OPTARG 
      ps -ef | grep -i $CHECK | wc -l 
      ;; 

     k) 
      KILL=$OPTARG 
      T2=$(ps -ef | grep -i "$KILL" | awk '{print $3,$5}' | sort -r +1 | sed 1d |awk '{print $1}') 
       for f in $T2; do 
         echo "killing $f" 
         kill $f 
       done 
      ;; 
     l) 
      LIST=$OPTARG 
      T2=$(ps -ef | grep -i "$LIST" | awk '{print $3,$5}' | sort -r +1 | sed 1d |awk '{print $1}') 
       for f in $T2; do 
         echo "PID $f" 
       done 
       ;; 
     ?) 
      usage 
      exit 
      ;; 
    esac 
done 

if [[ -z KILL ]] || [[ -z LIST ]] || [[ -z CHECK ]] 
then 
    usage 
    exit 1 
fi 

,也我不明白爲什麼當我調用沒有參數的腳本時,幫助不會顯示出來

回答

1

如果另一個程序在死亡時重新啓動它,則PID將會改變。守護進程實際上很常見。

usage不會被調用,因爲你正在檢查的KILL等是否是空的,而不是變量。只需在他們面前添加一個美元符號。