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

Print Friendly

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”

  1. Daroch March 4, 2011 at 2:42 pm #

    Hello
    First, thank you very much for the script.
    I’m new to this shellscripting and I found it very useful
    I wanted to make a query.

    Could you explain with more detail the commands you used to get the CPUSER and CPHOME variables?

    It would be possible to add the subdomains? to copy them 404.shtml and favicon.ico?

    Thanks in advance for your time
    Regards, Daroch

    • supersteve March 9, 2011 at 11:52 pm #

      Well,

      /bin/ls — /var/cpanel/users | /bin/grep -v “\`\|\.\|cpanel\|root\|mysql\|nobody”

      The above runs the ls command, then pipes (|) it to grep where anything that contains ` . cpanel root mysql or nobody is excluded
      It’s almost certainly possible to extend this for sub/addon domains… but I’m not sure where cpanel stores the data for sub/addon paths.

      Let me know if you find out!

    • Daroch April 9, 2011 at 12:01 am #

      Hi Supersteve:

      Thanks for your explanation.
      I’m don´t found the file where is the subdomain info. :-(
      I see that in WHM Interface, the option “list subdomains” use the folllowing code, to show the subdomain in the browser.

      I know some php, but this code is very strange to me, and I do not understand.
      Probably not be used for what we wanted to do, but just in case, I send you it.
      I hope that you can find useful
      Regards

      path: /scripts/listsubdomains

      #!/usr/bin/perl
      # cpanel – listsubdomains Copyright(c) 2010 cPanel, Inc.
      # All rights Reserved.
      # copyright@cpanel.net http://cpanel.net
      # This code is subject to the cPanel license. Unauthorized copying is prohibited

      BEGIN { unshift @INC, ‘/usr/local/cpanel’, ‘/scripts’; }

      use strict;

      use Cpanel::Config::LoadUserOwners ();
      use Cpanel::Config::userdata ();

      my $user = $ARGV[0];

      if ( !defined $user ) {
      &usage;
      }

      my @users = ($user);

      # Case 41981: For resellers, add their client users’ accounts
      my %OWNER;
      Cpanel::Config::LoadUserOwners::loadtrueuserowners( \%OWNER, 1, 1 );
      push @users, ( grep { $OWNER{$_} eq $users[0] && $_ ne $users[0] } keys(%OWNER) );

      foreach $user (@users) {
      print $user, “:\n”;
      my $subdomains_hr = Cpanel::Config::userdata::load_user_subdomains($user);

      for my $subdomain ( keys %$subdomains_hr ) {
      print “\t”, $subdomain, “\n”;
      }
      }

      sub usage {
      print <<EO_USAGE;
      Usage: listsubdomains user
      EO_USAGE
      exit 0;
      }

      :-)

Trackbacks/Pingbacks

  1. godaddy默认首页的设置方法 « 美国主机 - April 9, 2010

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

Leave a Reply


− 1 = three