BASH 16
Chk Apache Config By allan on 21st October 2025 11:59:47 AM
  1. #!/bin/bash
  2. ########################################################
  3. #
  4. #
  5. #   Copyright (c) 2025, CEGAL DK,  All rights reserved.
  6. #
  7. #   PURPOSE/NOTES
  8. #     Check Apache config files and status
  9. #
  10. #     arguments
  11. #
  12. #   REVISIONS:
  13. #   Ver        Date        Author           Description
  14. #
  15. #   1.0        2025.09.11  CEGAL.DK/amg     created
  16. #
  17. #########################################################
  18. progname=`basename $0`
  19. BASEDIR=$(dirname "$0")
  20. DATE=$(date +"%Y%m%d_%s")
  21. LOG=`basename $progname .sh`.log
  22.  
  23. function logthis()
  24. {
  25.   THISDATE=`date '+%Y.%m.%d %H:%M:%S'`
  26.   LOGTXT=$1
  27.   LOGFILE=$2
  28.  
  29.   echo "${THISDATE}  $LOGTXT "
  30.   echo "${THISDATE}  $LOGTXT " >> $LOGFILE
  31. }
  32.  
  33. function main()
  34. {
  35.   logthis "---> Start" $LOG
  36.   logthis "Check Apache config files and status" $LOG
  37.   # Check size of ssl.conf file
  38.   SSLCONF="/etc/httpd/conf.d/ssl.conf"
  39.   if [ -f $SSLCONF ]; then
  40.     FILESIZE=$(stat -c%s "$SSLCONF")
  41.     if [ $FILESIZE -gt 0 ]; then
  42.       logthis "File $SSLCONF exists and is not empty (size: $FILESIZE bytes)." $LOG
  43.       # Remove SSLCONF
  44.       rm -f $SSLCONF
  45.       logthis "File $SSLCONF has been removed." $LOG
  46.       # Create empty SSLCONF
  47.       touch $SSLCONF
  48.       logthis "Empty file $SSLCONF has been created." $LOG
  49.     else
  50.       logthis "File $SSLCONF exists but is empty! - Nothing to do" $LOG
  51.     fi
  52.     # Check Apache config syntax
  53.     logthis "Checking Apache config syntax:" $LOG
  54.     apachectl configtest >> $LOG 2>&1
  55.     if [ $? -eq 0 ]; then
  56.       logthis "Apache config syntax is OK." $LOG
  57.     else
  58.       logthis "Apache config syntax has errors!" $LOG
  59.       exit 1
  60.     fi
  61.     # Check Apache service status
  62.     logthis "Checking Apache service status:" $LOG
  63.     systemctl status httpd >> $LOG 2>&1
  64.     if [ $? -eq 0 ]; then
  65.       logthis "Apache service is running." $LOG
  66.     else
  67.       logthis "Apache service is not running! - Starting service" $LOG
  68.       systemctl start httpd >> $LOG 2>&1
  69.         if [ $? -eq 0 ]; then
  70.             logthis "Apache service has been started." $LOG
  71.         else
  72.             logthis "Failed to start Apache service!" $LOG
  73.             exit 1
  74.         fi
  75.     fi
  76.   fi
  77.   logthis "End <---" $LOG
  78. }
  79.  
  80. ## Run main()
  81. main
  82. exit 0

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.