LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script for httpd (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-httpd-709412/)

urgido 03-05-2009 11:39 AM

shell script for httpd
 
Hi, I make this script but I'm not sure if this is correct.

Code:

#!/bin/bash

set -e

SERVICE="httpd"

while true; do
if ps -ef | grep -q $SERVICE; then
 lsof | grep IPv | grep 80 | awk '{print $2}' | xargs -l -i kill -9 {} 
 /usr/sbin/$SERVICE -k restart
fi
 sleep 2
done

I MAKE THIS SCRIPT FOR RESTART MY APACHE DAEMON IF THIS IS DOWN BUT ELSE FOR KILL ALL THE PROCESS THAT USE THE 80 PORT.

I HOPE YOUR HELP. Regards

I USE DEBIAN 4.*

frieza 03-05-2009 11:46 AM

try replacing
/usr/sbin/$SERVICE -k restart
with
/etc/init.d/$SERVICE restart

T74marcell 03-05-2009 07:18 PM

This script runs forever - doesn't it make more sense to create a simpler script that is run as a cron job? Then you will have much better control about the timing intervals. Your script won't eat a lot of system resources, but it's just bad practice to use endless loops with sleep calls.

----------
T74marcell

Arch Linux

urgido 03-06-2009 04:32 PM

I don't user cron jobs but is a good idea. I have cron jobs of directadmin control panel so this can be work?

anomie 03-06-2009 04:41 PM

Quote:

Originally Posted by urgido
Code:

#!/bin/bash

set -e

SERVICE="httpd"

while true; do
if ps -ef | grep -q $SERVICE; then
 lsof | grep IPv | grep 80 | awk '{print $2}' | xargs -l -i kill -9 {} 
 /usr/sbin/$SERVICE -k restart
fi
 sleep 2
done

I MAKE THIS SCRIPT FOR RESTART MY APACHE DAEMON IF THIS IS DOWN BUT ELSE FOR KILL ALL THE PROCESS THAT USE THE 80 PORT.

This is insane. You're restarting httpd every 2 seconds.

On top of that, your non-precise grep 80 statement could match anything that has, e.g. "80" in the PID. So you'd violently kill something you did not intend to kill, and then start only httpd.

All your script really needs to do is this:
Code:

# pgrep httpd >/dev/null || /etc/init.d/httpd start
(or whatever Debian uses to start services)

anomie 03-06-2009 04:43 PM

I'd also add that this is a poor approach to service management, IMO. If httpd is crashing regularly, diagnose and solve the problem. You should also deploy some form of service monitoring - e.g. Nagios.

kyle_p 03-06-2009 07:02 PM

Quote:

Originally Posted by anomie (Post 3467340)
All your script really needs to do is this:
Code:

# pgrep httpd >/dev/null || /etc/init.d/httpd start
(or whatever Debian uses to start services)

I love this post. An advanced scripting problem resolved with beautiful simplicity. :)

urgido 03-07-2009 12:21 PM

so my script is wrong and the correct shell script is just: # pgrep httpd >/dev/null || /etc/init.d/httpd start ???

anomie 03-07-2009 11:31 PM

Your script is causing a lot of unnecessary activity (and probably service interruptions) for the httpd daemon. A closely related question is: why is this script you're proposing necessary? Is httpd regularly crashing?

The one-liner I suggested could run as a cronjob every 5 minutes or so.

urgido 03-08-2009 04:42 AM

Is httpd regularly crashing?

YES =(

I'm newbie on Linux. Frequently it's necessary to kill the process of httpd and up again.

urgido 03-08-2009 04:52 AM

LOOK THIS:

IN THIS MOMENT MY HTTPD SERVICE IS CRASHING SO I PROCCED TO CHECK THE ISSUE:

box:~# lsof -w -n -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
h 8858 apache 3u IPv6 1684609 TCP *:www (LISTEN)

wtf? h command?

So if I try to run my httpd service the system print an error like this: You can't use 80 port because is in use.

So for solve the problem I use the follow commands:

box:~# kill -9 8858
box:~# /usr/sbin/httpd -k start
box:~# lsof -w -n -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd 21138 root 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21139 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21140 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21141 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21142 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21143 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21144 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21145 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21146 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21147 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21148 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21149 apache 3u IPv6 1761832 TCP *:www (LISTEN)
httpd 21150 apache 3u IPv6 1761832 TCP *:www (LISTEN)
box:~#

AND ALL IS OK! =/ ... But this happeds frecuently so I need a shell script to do this. Regards

SORRY IF MY ENGLISH IS BAD.

unSpawn 03-08-2009 07:48 AM

Like Anomie already said: if your httpd is crashing regularly combatting symptoms won't make the problem go away. You need to diagnose and solve the problem instead. Do your system and http daemon access and error logs show warnings or errors?

urgido 03-08-2009 02:01 PM

error_log

[Sat Mar 07 00:10:15 2009] [notice] Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8c DAV/2 PHP/5.2.9 configured -- resuming normal operations

[Sat Mar 07 00:11:02 2009] [notice] caught SIGTERM, shutting down

anomie 03-09-2009 04:48 PM

Quote:

Originally Posted by urgido
[Sat Mar 07 00:11:02 2009] [notice] caught SIGTERM, shutting down

It looks like a program (maybe another one of your shell scripts?) may be sending a kill signal to httpd.

What I'd suggest is editing httpd.conf so that you have the directive:
Code:

LogLevel info
Then reload or restart httpd. Finally, wait until httpd crashes again, and then post the last 30 or so lines of error_log here.

---

Also, are you the only sysadmin on this server? Is anyone else root or a sudoer besides you?

anomie 03-09-2009 04:52 PM

Quote:

Originally Posted by urgido
IN THIS MOMENT MY HTTPD SERVICE IS CRASHING SO I PROCCED TO CHECK THE ISSUE:

box:~# lsof -w -n -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
h 8858 apache 3u IPv6 1684609 TCP *:www (LISTEN)

wtf? h command?

That's definitely odd. Next time you see this h (?) daemon, also post the results of:
# lsof -p <pid_here>

(... in addition to the other items above.)


All times are GMT -5. The time now is 03:58 PM.