Home Ask Login Register

Developers Planet

Your answer is one click away!

anarki February 2016

bash script to restart apache if apachectl passes

I'm working on a bash script that rewrites vhost files and then restarts apache to pick up the changes. It all works fine but I want to check the status of apachectl before restarting apache in case the vhost files have syntax errors.

I know how to run apachectl -S from the command line, but I want my script to work like:

if(apachectl passes){

   service httpd restart


I guess I could just parse the output and look for 'Syntax OK' but is there a better way?


Joao Morais February 2016

You can just:

if apachectl -S; then
  service httpd restart
  // action if config parsing failed

apachectl will result a non zero exit code, jumping to the else branch.

Post Status

Asked in February 2016
Viewed 3,723 times
Voted 14
Answered 1 times


Leave an answer

Quote of the day: live life