Linux: How to auto send email to users exceeding quota

If you want to limit your users disk usage with a quota and notify them automatically whenever they exceet their quota you may write a script like the one below and add it to crontab to run periodically.

  • disk_quota.sh
    1. ADMIN="admin@yourdomain.com"
      KOTA=1     # set the quota
      EMAILBODY="/root/scripts/mesage.txt"
      
      function notify_users(){
      while read LINE
      do
      DISK=$(echo $LINE | awk '{ print $1 }')  # users disk usage
      KUL=$(echo $LINE | awk '{ print $2 }' | cut -d '/' -f 3-)     # username
      
      MESSAGE="Dear $KUL, this email was generated automatically by yourdomain.com web server. You have exceeded the quota which is $KOTA GB . Your disk space usage is: $DISK. Please decrease the disk space usage below the quota.For your question you may contact to $ADMIN."
      
      if [ -f $EMAILBODY ] ;
      then
      rm -f $EMAILBODY
      fi
      
      echo -e $MESSAGE >> $EMAILBODY
      echo -e "\n\n\n" >> $EMAILBODY
      echo -e $MESAJ >> $EMAILBODY
      mail -s "users who exceets the quota" "admin@yourdomain.com" < $EMAILBODY    ########## if you want to send email to your admin
      # mail -s "users who exceets the quota" "$KUL@yourdomain.com" < $EMAILBODY  ########## if you want to send email directly to the user
      done<$FILE
      }
      ##########end of the function####################
      
      notify_users
      
  • Add the following line to crontab to run the script every hour
    1. 0 * * * * /root/scripts/disk_quota.sh>/dev/null
      

    Bir Cevap Yazın

    E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir


    one + = 7

    Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

    Yandex.Metrica