Skip to content

Instantly share code, notes, and snippets.

@vladkras
Created June 12, 2019 03:17
Show Gist options
  • Save vladkras/4e6bf0cb53eb9c623b19426da0892ebb to your computer and use it in GitHub Desktop.
Save vladkras/4e6bf0cb53eb9c623b19426da0892ebb to your computer and use it in GitHub Desktop.
Test config and reload Apache in one command
function reload () {
STATUS=$(sudo apache2ctl configtest 2>&1)
OK=$(echo $STATUS | grep "OK" | wc -l)
if [[ $OK -eq 1 ]]; then
echo "reloading Apache..."
sudo service apache2 reload
else
echo "$STATUS"
LINEOF=$(echo "$STATUS" | grep -Eo "line ([0-9]+) of [^:]+")
if [ ! -z "$LINEOF" ]; then
POS=$(echo "$LINEOF" | cut -d' ' -f2)
FILE=$(echo "$LINEOF" | cut -d' ' -f4)
echo "Open $FILE:$POS to edit [y/n]?"
read y
if [[ "$y" == "y" ]]; then
sudo vim +$POS $FILE
fi
fi
fi
}
@vladkras
Copy link
Author

vladkras commented Jun 12, 2019

This function tests Apache config for errors and immediately reloads If none found, or requests if you want to edit specific line in your config file where problem was found.

Add this function to your ~/.bashrc file, relogin and type reload (I use vim and apache2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment