Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-27-2011, 12:03 PM   #1
LQ Newbie
Registered: Sep 2011
Posts: 19

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

I have a script which has the following line:

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:

service apache2 start
if (( $? == 0 )); then
 ...mail user "apache started successfully"
 ...mail user "apache start failed, reverting to old settings"
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
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
You could capture the output and helpfully include it in the error mail:
buf=$( service apache2 start 2>&1 )
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"
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
LQ Newbie
Registered: Sep 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled

Thanks I can definitely do something with that!


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 / openSUSE 14 04-18-2006 12:36 PM
Starting Apache2 using Service Command liaaam Linux - Software 2 06-14-2004 06:06 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:11 AM.

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