2014-02-10 24 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等是否是空的,而不是变量。只需在他们面前添加一个美元符号。