Bash Script to place 404.shtml and favicon.ico in home directories

Print Friendly, PDF & Email

OK, so you’ve noticed how your error_log files are just full to busting with 404 errors for favicon.ico and 404.shtml? Annoying isn’t it, especially when you have a lot of activity on a server, as these files can mushroom out of control.

This script will go through each user account’s home directory, and where it doesn’t find then, it will place a copy of 404.shtml and favicon.ico for you.

To use this script, ¬†create a 1×1 pixel transparent favicon.ico file and a basic 404.shtml file and placed them in the /root/cpanel3-skel/public_html/ directory. Therefore these files will also automatically get placed into any new accounts created. If you want to enforce your branding, you could always place a branded favicon.ico file in there, but you’ll probably just annoy your users.

As always – use this script entirely at your own risk. This reduced the size of my weekly error logs by more than half.

#!/bin/bash
IFS="$"
 
# Delay in Seconds Between Accounts
typeset -i DELAY=1
 
# Just a safe starting point
cd /home
 
##########################################################
##  SCRIPT BEGINS HERE                                  ##
##########################################################
 
unset CPUSER CPHOME
 
/bin/ls -- /var/cpanel/users | /bin/grep -v "\`\|\.\|cpanel\|root\|mysql\|nobody" | while read CPUSER; do
   CPHOME="$(/bin/grep "${CPUSER}:" /etc/passwd | cut -d':' -f6)/public_html"
   echo -e "\nChecking ${CPHOME} Updating ${CPUSER}'s account ..."
   sleep ${DELAY}   # Slow things down so you can see dialog message
   if [ -d ${CPHOME} ]; then
 
     ###########################################
     ## Put dummy favicon and 400.shtml       ##
     ###########################################
 
     echo "Looking for missing favicon and 400.shtml for ${CPUSER} ..."
     if [ ! -e ${CPHOME}/favicon.ico ]
     then
       echo "${CPUSER} missing favicon.ico - copying to ${CPHOME}/"
       /bin/cp /root/cpanel3-skel/public_html/favicon.ico ${CPHOME}/
       /bin/chown ${CPUSER} ${CPHOME}/favicon.ico
       /bin/chgrp ${CPUSER} ${CPHOME}/favicon.ico
       /bin/chmod 644 ${CPHOME}/favicon.ico
     fi
     if [ ! -e ${CPHOME}/400.shtml ]
     then
       echo "${CPUSER} missing 400.shtml - copying to ${CPHOME}/"
       /bin/cp /root/cpanel3-skel/public_html/400.shtml ${CPHOME}/
       /bin/chown ${CPUSER} ${CPHOME}/400.shtml
       /bin/chgrp ${CPUSER} ${CPHOME}/400.shtml
       /bin/chmod 644 ${CPHOME}/400.shtml
     fi
 
   fi
done

Tags: , , , ,

4 Responses to “Bash Script to place 404.shtml and favicon.ico in home directories”