LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH: service apache2 start => check apache2 response (http://www.linuxquestions.org/questions/linux-newbie-8/bash-service-apache2-start-%3D-check-apache2-response-910491/)

dragonetti 10-27-2011 12:03 PM

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?

catkin 10-29-2011 01:10 AM

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.

dragonetti 10-29-2011 12:01 PM

@catkin

Thanks I can definitely do something with that!


All times are GMT -5. The time now is 02:35 PM.