你好我创建了一个脚本来杀死按年龄排序的进程,但是每次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
并且我也不明白为什么当我在没有参数的情况下调用脚本时,帮助没有显示
答案 0 :(得分:1)
如果另一个程序在被杀死时重新启动它,PID将会改变。这实际上与守护进程很常见。
usage
永远不会被调用,因为您正在检查字符串 KILL
等是否为空,而不是变量。只需在他们面前添加一个美元符号。