如何修改脚本以排除某些进程?

时间:2014-04-21 11:16:38

标签: bash time process kill

欢迎,我有一个简短的脚本来杀死比UID大于指定时间的工作。如何从杀戮中排除例如mc命令?

#!/bin/bash
#
#Put the minimum(!) UID to kill processes
UID_KILL=500

#Put the time in seconds which the process is allowed to run below
KILL_TIME=300

KILL_LIST=`{
    ps -eo uid,pid,lstart | tail -n+2 |
    while read PROC_UID PROC_PID PROC_LSTART; do
SECONDS=$[$(date +%s) - $(date -d"$PROC_LSTART" +%s)]
if [  $PROC_UID -ge $UID_KILL -a $SECONDS -gt $KILL_TIME ]; then
    echo -n "$PROC_PID "
fi
done
}`

#KILLING LOOP
while sleep 1
do
    if [[ -n $KILL_LIST ]]
    then
        kill $KILL_LIST
    fi
done

1 个答案:

答案 0 :(得分:0)

改变内部命令:

ps -eo comm,uid,pid,lstart | tail -n+2 | grep -v '^your_command' | ...

这将从列表中排除'your_command'

有关STANDARD FORMAT SPECIFIERS的更多信息,请参阅man ps中的ps -o