brunoalves Posted August 11, 2022 Posted August 11, 2022 Pessoal, pegando o bonde andando aqui no portal, peguei um script que reinicia automaticamente um serviço quando o load do servidor atinge uma porcentagem. Este é o script #!/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 restart echo `uptime` | mail -s "ALERT - Load HIGH `hostname`" email@site.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 , ok, o script é simples, eu entro no SSH e executo ./meuscript.sh e quando o load atinge 20% ele reinicia o mysql, mas o script nao funciona se meu ssh desconectar por exemplo Queria saber, como executar este script para que ele fique sempre rodando, pensei em agendar um cron, porem acredito que sempre que o cron for executado ele ira deixar um processo deste script ativo. como faço? 0 Quote
pluginscpanelwhm Posted August 11, 2022 Posted August 11, 2022 11 horas atrás, brunoalves disse: Pessoal, pegando o bonde andando aqui no portal, peguei um script que reinicia automaticamente um serviço quando o load do servidor atinge uma porcentagem. Este é o script #!/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 restart echo `uptime` | mail -s "ALERT - Load HIGH `hostname`" email@site.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 , ok, o script é simples, eu entro no SSH e executo ./meuscript.sh e quando o load atinge 20% ele reinicia o mysql, mas o script nao funciona se meu ssh desconectar por exemplo Queria saber, como executar este script para que ele fique sempre rodando, pensei em agendar um cron, porem acredito que sempre que o cron for executado ele ira deixar um processo deste script ativo. como faço? Olá, É só fazer sem loop #!/bin/bash SHELL=/bin/sh PATH=/sbin:/usr/sbin:/usr/bin:/bin loadLimit=20 load=$(awk '{print $1}' /proc/loadavg | cut -d '.' -f1) if [ "$load" -gt "$loadLimit" ]; then echo ""; service mysql restart else echo ""; fi exit Ai você coloca no cron pra rodar a cada minuto */1 * * * * /bin/load_alto.sh 1 Quote
Hermano Posted August 11, 2022 Posted August 11, 2022 Já tentou executar o script em BG com o nohup? Ficaria algo como: # nohup ./meuscript.sh & https://linuxhint.com/how_to_use_nohup_linux/ Outra alternativa é executar o script dentro de uma "screen" ou transforma-lo em um serviço usando o systemd. 0 Quote
brunoalves Posted August 13, 2022 Author Posted August 13, 2022 @pluginscpanelwhm Testei aqui e aparentemente esta funcionando do jeito que eu queria, so mais uma dúvida, e se eu quisesse que voce gerado um log dos horarios que o mysql foi reiniciado, isto e possivel? muito obrigado. @Hermano Valeu pela dica!!! 0 Quote
bdlc Posted August 13, 2022 Posted August 13, 2022 (edited) Bruno, É simples, basta adicionar algo do tipo na linha acima do comando de reiniciar: date >> /[caminho]/mysql_log_reiniciado.txt Dessa forma a cada execução do comando de reiniciar será inserido no arquivo indicado a data e horário. Abraço, Bruno. Edited August 13, 2022 by bdlc 0 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.