Jump to content

Reiniciar serviço no linux baseado no load das CPUs


Recommended Posts

Olá Pessoal, 

Alguma alma aqui sabe como reiniciar um ou mais serviços específicos baseado no consumo atual de CPU na máquina? Seria no Linux.

 

É um projetinho aqui bem específico, que seria necessário realizar algo nesse sentido.

Link to comment
Share on other sites

Vittor,

Segue abaixo um script que faz isso, você define o load que deseja e, quando ele ocorrer, o script roda determinados comandos definidos por você. Você pode subir ele em segundo plano com o "&" ou rodar por tarefa agendada, vai depender da sua necessidade.

Abraço, Bruno.

#!/bin/bash
#The minimum load average at 1 minute to trigger an event
LIMIT="20"
#run counter, is only used to see that the script is still running.
RUNCOUNT="0"
#enter an infinite loop
while true
do
#echo "Run count: $RUNCOUNT"
RUNCOUNT=$[$RUNCOUNT + 1]
#Get the current 1 min average load
LOAD1MIN="$(uptime | awk -F "load average:" '{ print $2 }' | cut -d, -f1 | sed 's/ //g')"
#set true or false, if the load is above the limit specified at the top of the script
RESULT=$(echo "$LOAD1MIN > $LIMIT" | bc)
#If the load is above the trigger limit, then break free of the loop to end the script
if [ "$RESULT" == "1" ]; then
        echo "Load is above $LIMIT!"
        #Add the commands here that you want to execute when the 1min average load is above $LIMIT. One command per line
        #break out of the loop
        #You can remove this break, if you want the script to re-run automatically even when the load has been above $LIMIT.
        #in my case, i needed it to stop once the load was above $LIMIT so i added this break.
	service mysql stop
        echo `uptime` | mail -s "ALERT - Load HIGH `hostname`" email@email.com
        break
else
        echo "Load not above $LIMIT"
fi
#sleep for 1 second
sleep 1
#clear the screen to make it all pretty
clear
#End of loop, if this it hit, then return to top of loop and start over
done
#exit the script, since the load was above $LIMIT we don't need to run it anymore
exit 0

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...