Pbs_nice.sh

2020-11-17  本文已影响0人  多一点儿空气

地址:https://www.jianshu.com/p/8b4ee9cee16c

#!/bin/bash
case "$1" in
    start)
(while [ 0 -eq 0 ]
do
  sleep 30
  old_IFS=$IFS; IFS=$'\n'; temp=(`ps -ef | sed '/^root/d' | sed '1d' | sort -k 3 | awk '{printf "%s %s %s %s\n", $1, $2, $3, $4}'`); num=${#temp[@]}; IFS=$old_IFS
  usage=2; pbs_id=`/opt/torque-6.1.2/bin/qstat -r | sed -n '6,$p' | awk '{print $5}'`; all=""; pbs=""
  > /opt/log
  if [[ -n "$pbs_id" ]]; then
    for ((i=0;i<num;i++))
    do
      ID=(`echo ${temp[i]}`)
      if [[ "${ID[2]}" != "1" ]]; then
        for var in $pbs_id
        do
          if [[ "${ID[2]}" == "$var" ]]; then pbs_id=$pbs_id" "${ID[1]}; fi
        done
      fi
      if [[ "${ID[3]}" -gt "$usage" ]]; then all=${ID[0]}.${ID[1]}" "$all; fi
      for var in $pbs_id
      do
        if [[ "${ID[1]}" == "$var" && "${ID[2]}" != "1" && "${ID[3]}" -gt "$usage" ]]; then pbs=${ID[0]}.${ID[1]}" "$pbs; fi
      done
    done

    pbs=`echo $pbs | tr ' ' '\n' | sort`; all=`echo $all | tr ' ' '\n' | sort`; echo -en $pbs"\nPBS\n" >> /opt/log
    pbs_user=(`echo $pbs | sed 's#[.][0-9]*\>##g' | awk '{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) printf "%s.%s ", i,array[i]}' | tr' ' '\n' | sort`); n=${#pbs_user[@]}; core=0
    for ((i=0;i<n;i++))
    do
      u=(`echo ${pbs_user[i]} | tr '.' ' '`)
      user_id=`echo $pbs | tr ' ' '\n' | grep ${u[0]}[.][0-9]* | sed "s/[^ ]*[.]//g"`
      echo "${pbs_user[i]}: "$user_id >> /opt/log
      renice -5 $user_id > /dev/null; j=1
      for var in $user_id; do if [[ $j -le 38 ]]; then taskset -pc $core $var > /dev/null; ((j++)); ((core++)); else taskset -pc 0-39 $var > /dev/null; fi; done
    done
    echo core: "$core" >> /opt/log

    operation=`comm -13 <(echo "$pbs") <(echo "$all")`; echo -en $operation"\nOPE\n" >> /opt/log
    ope_user=(`echo $operation | sed 's#[.][0-9]*\>##g' | awk '{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) printf "%s.%s ", i,array[i]}' | tr ' ' '\n' | sort`); n=${#ope_user[@]}
    for ((i=0;i<n;i++))
    do
      u=(`echo ${ope_user[i]} | tr '.' ' '`)
      user_id=`echo $operation | tr ' ' '\n' | grep ${u[0]}[.][0-9]* | sed "s/[^ ]*[.]//g"`
      echo "${ope_user[i]}: "$user_id >> /opt/log
      renice 1 $user_id > /dev/null; j=1
      for var in $user_id; do if [[ $j -le 3 ]]; then if [[ $core -le 38 ]]; then taskset -pc $core $var > /dev/null; ((j++)); ((core++)); elsetaskset -pc 39 $var > /dev/null; ((j++)); fi; else kill $var; fi; done
    done
    echo "core: "$core >> /opt/log
  fi
done)&

    ;;

    stop)
echo -n "Shutting down Pbs_nice Scheduler: "
killproc Pbs_nice
echo 'stop Pbs_nice'
    ;;
    status)
status Pbs_nice
    ;;
    restart)
$0 stop
$0 start
    ;;
    *)
echo "Usage: Pbs_nice {start|stop|restart|status}"
    ;;

esac
上一篇下一篇

猜你喜欢

热点阅读