# !/bin/bash
# Made by Kriksa
# Скрипт перезагружает демон aldd, если процесс aldd занимает более 60% памяти 
cron_script="/etc/cron.d/aldd_check"
right_path="/usr/local/bin/aldd_restart.sh"

[ $UID -ne 0 ] && echo "You have to be with admin rights!" && exit 1

[ -x "/etc/init.d/aldd" ] || { echo "ald is not installed on this computer"; exit 1; }

if [ ! -f $right_path ]
then
    echo "Put the script to /usr/local/bin/ and run it there!"
    exit 1
fi

if [ -e $cron_script ]
then
    true
else
    echo "*/1 * * * *   root    $right_path" > $cron_script
fi

proc_overflow=`top -b -n1 | grep aldd | awk '{if ($2=="aldd") {print ($9)}}' | awk -F, '{print($1)}'`
if [ $proc_overflow -gt 60 ]
then
    sudo /etc/init.d/aldd restart
fi

