LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2009, 11:39 AM   #1
urgido
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Rep: Reputation: 0
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.*
 
Old 03-05-2009, 11:46 AM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,225

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
try replacing
/usr/sbin/$SERVICE -k restart
with
/etc/init.d/$SERVICE restart
 
Old 03-05-2009, 07:18 PM   #3
T74marcell
Member
 
Registered: Mar 2009
Posts: 102

Rep: Reputation: 18
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

Last edited by T74marcell; 03-14-2009 at 12:59 AM.
 
Old 03-06-2009, 04:32 PM   #4
urgido
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
I don't user cron jobs but is a good idea. I have cron jobs of directadmin control panel so this can be work?
 
Old 03-06-2009, 04:41 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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)
 
Old 03-06-2009, 04:43 PM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 03-06-2009, 07:02 PM   #7
kyle_p
LQ Newbie
 
Registered: Mar 2009
Location: Canada
Distribution: CentOS, RHEL*, SuSE*, *
Posts: 12

Rep: Reputation: 0
Quote:
Originally Posted by anomie View Post
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.
 
Old 03-07-2009, 12:21 PM   #8
urgido
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
so my script is wrong and the correct shell script is just: # pgrep httpd >/dev/null || /etc/init.d/httpd start ???
 
Old 03-07-2009, 11:31 PM   #9
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.
 
Old 03-08-2009, 04:42 AM   #10
urgido
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Is httpd regularly crashing?

YES =(

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

Last edited by urgido; 03-08-2009 at 04:46 AM.
 
Old 03-08-2009, 04:52 AM   #11
urgido
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 03-08-2009, 07:48 AM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,359
Blog Entries: 55

Rep: Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546Reputation: 3546
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?
 
Old 03-08-2009, 02:01 PM   #13
urgido
LQ Newbie
 
Registered: Dec 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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

Last edited by urgido; 03-08-2009 at 02:10 PM.
 
Old 03-09-2009, 04:48 PM   #14
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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?
 
Old 03-09-2009, 04:52 PM   #15
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
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.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration