LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 10-27-2011, 12:03 PM   #1
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 18

Rep: Reputation: Disabled
BASH: service apache2 start => check apache2 response


I have a script which has the following line:

Code:
service apache2 start
Every time you start/stop/... apache, apache always gives some form of feedback.

Is it possible for me to catch that feedback in the BASH script and do an if/else action on it?

From the o'reilly BASH cookbook I could make something like this:

Code:
service apache2 start
if (( $? == 0 )); then
 ...mail user "apache started successfully"
else
 ...mail user "apache start failed, reverting to old settings"
fi
Or is there a better route?

Last edited by dragonetti; 10-27-2011 at 12:06 PM.
 
Old 10-29-2011, 01:10 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
You could capture the output and helpfully include it in the error mail:
Code:
buf=$( service apache2 start 2>&1 )
rc=$?
case $buf in
    *'<partial match to normal output>'* )
        ...mail user "apache started successfully"
        ;;
    * )
        ...mail user "apache start failed, reverting to old settings.
  Return code: $rc
  Output: $buf"
esac
The downside of this approach is that if the normal service apache2 start output changes the script has to be re-written.

Last edited by catkin; 10-29-2011 at 01:12 AM. Reason: Added missing stderr redirect
 
1 members found this post helpful.
Old 10-29-2011, 12:01 PM   #3
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
@catkin

Thanks I can definitely do something with that!
 
  


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
[SOLVED] Compiling my own apache2 on debian -- no /usr/sbin/apache2 ??? jippen Linux - Server 2 11-30-2009 04:21 AM
Apache2 - have to restart apache2 process every 10 mins to clear /tmp directory.... levi dehaan Linux - Software 8 09-01-2009 07:34 PM
apache2 closing connection , no response entz Linux - Server 1 04-22-2009 09:53 AM
SuSe Apache2 error: cannot adjust 'apache2' service sir_shunt Suse/Novell 14 04-18-2006 12:36 PM
Starting Apache2 using Service Command liaaam Linux - Software 2 06-14-2004 06:06 AM


All times are GMT -5. The time now is 04:38 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration