• About Me
  • Skills
  • Education
  • Employment
  • Blog
  • Contact
  • Open Source
    • WordPress
      • 3B Meteo Plugin
      • Facebook Like and Send
      • Google Monthly XML Sitemap
      • Google News Sitemap
      • Google Plus Comments
      • Google Plus One Button
      • I don’t endorse Google
      • I don’t endorse Wikipedia
      • I don’t endorse WIkipedia
      • Ottimizzare WordPress per renderlo più veloce
      • Rich Category Editor
      • Save To Google Drive
      • Sociable Italia
      • Third Party Cookie Eraser
      • Twitter Follow Button
      • WP instantempo
      • WP Super Secure and Fast htaccess
    • Plone SEO – collective.perseo
  • Creations
    • Web Marketing
    • SEO
    • WPO
    • SEO Stupidity
    • Webmaster Superhero
    • Il Vero SEO

Shell Script per riavviare Apache in automatico quando va down

13 Agosto 2014 / By Andrea Pernici
  • Home
  • Shell Script per riavviare Apache in automatico quando va down
restart apache

Ogni tanto può succedere che Apache vada giù senza che noi riusciamo a darne una spiegazione anche dopo aver controllato i log. Questa cosa mi capita spesso su alcune macchine di test e dunque ho cercato un po’ in rete una soluzione veloce, semplice per aggirare il problema. Aggirare perché ovviamente non è la soluzione migliore, ma se siamo su una macchina senza troppe pretese allora può tornare utile.

Lo script che ho utilizzato è quello trovato su Cybercity ed è il seguente.

  1. #!/bin/bash
  2. # Apache Process Monitor
  3. # Restart Apache Web Server When It Goes Down
  4. # ————————————————————————-
  5. # Copyright (c) 2003 nixCraft project <http://cyberciti.biz/fb/>
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # ————————————————————————-
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # ————————————————————————-
  11. # RHEL / CentOS / Fedora Linux restart command
  12. RESTART=“/sbin/service httpd restart”
  13. # uncomment if you are using Debian / Ubuntu Linux
  14. #RESTART=“/etc/init.d/apache2 restart”
  15. #path to pgrep command
  16. PGREP=“/usr/bin/pgrep”
  17. # Httpd daemon name,
  18. # Under RHEL/CentOS/Fedora it is httpd
  19. # Under Debian 4.x it is apache2
  20. HTTPD=“httpd”
  21. # find httpd pid
  22. $PGREP ${HTTPD}
  23. if [ $? -ne 0 ] # if apache not running
  24. then
  25. # restart apache
  26. $RESTART
  27. fi

Lo script non fa altro che usare pgrep per controllare che effettivamente ci siano processi Apache/httpd attivi e in caso contrario chiede un riavvio.

Per farlo andare in automatico basta impostare un cronjob ogni X minuti facendo crontab -e e inserendo la seguente linea

*/5 * * * * /path/to/script.sh >/dev/null 2>&1

In questo modo se Apache sarà in stato di inattività verrà riavviato.

Quando il restart non funziona

Ci sono anche casi più complessi dove un semplice ‘restart’ potrebbe non bastare.
In questi casi quello che possiamo fare è seguire la seguente serie di comandi.

Killare tutti i processi Apache e dunque fare

# ipcs -s apache 
# ipcs -s | grep apache | perl -e 'while () { @a=split(/\s+/);print `ipcrm sem $a[1]`}' 
# cd /var/lock/subsys
# rm apache2
# /etc/init.d/apache2 restart
Tags
Linux, script, shell
About Me
about-me
Andrea Pernici

Chief Technology Officer at Search On Media Group and 3BMeteo.com

Categories
  • Apache 2.2
  • Blog e Social
  • CSS
  • Featured Articles
  • Google
  • Grafica
  • Linux
  • Marketing
  • Politica
  • Programmazione
  • Seo
  • Sicurezza
  • Sport
  • Varie
  • Viaggi
  • Video
  • WebServer
Popular Post
  • Corso HTML per SEOlunedì - Dicembre 05, 2022
  • Antonio Jelich e Pallamano 85. La terza di campionato.lunedì - Marzo 15, 2021
  • How to Dominate “Google In-The News” Box (aka “Top Stories”)…lunedì - Maggio 08, 2017
  • WordPress Brute Force Attacks: lock your wp-login.php with ModSecuritymercoledì - Ottobre 28, 2015
  • All the small things that make a great productmartedì - Ottobre 27, 2015
Tags
Andrea Pernici apache Bug Google Convegno gt Convegno SEO Design Errori Google Eventi SEO Facebook Facebook Like Google Google Maps Google News Sitemap Google Plus Google profile gtConference gt study days instantempo Instant Previews Joomla Linkedin Linux Loghi Marketing Matt Cutts Meteo migliori tweet Open Source Ottimizzare Wordpress Page Speed Rich Snippet script Seo SERP shell Significati Nascosti Sociable italia Social Network Twitter Usabilità Web Performance WebProNews Web Server Apache Wordpress Wordpress plugin
Recent Comment
  • Come fare Redirect 301 di una pagina web giovedì - Maggio 06, 2021 08:44 am
  • Come fare Redirect 301 di una pagina web martedì - Maggio 04, 2021 02:03 pm
  • Come fare Redirect 301 di una pagina web sabato - Aprile 10, 2021 02:05 pm
  • Come fare Redirect 301 di una pagina web venerdì - Aprile 09, 2021 11:02 pm
  • Come fare Redirect 301 di una pagina web giovedì - Aprile 01, 2021 10:08 am
© 2017 Andrea Pernici - SEO, Design & Technology as attitudes - Me on freebase - Privacy Policy