2014-02-10 5 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가 변경됩니다를 표시하지 않는 이유를 이해하지 않습니다. 이것은 데몬과 실제로 매우 일반적입니다. 당신이 문자열KILL 등이 변수이 비어 있지 여부를 확인하고 있기 때문에

usage가 호출되지 않습니다. 그냥 앞에 달러 기호를 추가하십시오.

관련 문제