-1

With heavy trafic, it happen that's my HTTP service crash on my dedicated server. Then, I have to restart httpd service manually.

I just search for a shell script that's detect if HTTP service is OK - If not : restart httpd.

Thanks a lot if you had this in your pocket ...

voretaq7
  • 79,879
  • 17
  • 130
  • 214
  • Welcome to ServerFault, user1740613! (Searching in pockets - no scripts at the moment, sorry) Think you stand better chances of receiving a shell code snippet at Un*x SE where there is no requirement for posters to be sysadmins in a **professional** capacity. However, even there (as elsewhere on the StackExchange family of sites) some prior effort before asking other people to do your job for you is required. You may also want to investigate **why** your service crashes and **how** you can prevent this from happening. Remember, curing the cause is preferred to treating symptoms. – Deer Hunter Jan 20 '13 at 16:13

3 Answers3

3

This is not a solution to your problem. You need to find the real cause of this problem to fix it.

Anyway, you can try monit. It is a daemon to monitor your services and execute configured actions such as restart.

Khaled
  • 36,533
  • 8
  • 72
  • 99
  • Thanks. What I search for the moment is completing a script (CRON launched every 10 minutes for example). Is this script well written ? #!/bin/sh process = 'httpd' if ps ax | grep -v grep | grep $process then echo "$process is alive." else echo "$process is dead, but will be launched." /etc/init.d/httpd restart fi – user1740613 Jan 20 '13 at 14:28
  • 1
    Monit will do it. No need to for cron jobs. – Khaled Jan 20 '13 at 14:29
1

If you intend to use a shell script ...

#!/bin/bash

httpd_init="/etc/init.d/httpd"
check1="$(pgrep -f httpd)"
check2="$($httpd_init status | grep -E is[^not]running)"

[[ -n $check1 && -n $check2 ]] && echo "httpd is alive!" || { echo "httpd is dead!"; $httpd_init start; }

exit $?

This script performs a simple check on your httpd pids (using pgrep) as well as a small regex check on the response string of "/etc/init.d/httpd status" (which, in case of a service failure, will be "is stopped" of course).

If the service is dead, the script will bring it back to life.

Hope this helps!

nj64
  • 11
  • 3
0

Many distributions are migrating to systemd for init, that handles this sort of issues automatically. Sorry, I know it's not an inmediate solution to your woes.

vonbrand
  • 1,149
  • 2
  • 8
  • 16