Tag Archives: bash scripting

Exim Queue Management Tutorial

So, you have got yourself a shiney new dedicated or VPS server with cPanel or DirectAdmin or some such installed. It probably won’t be long before you have to start checking the exim mail logs and the exim mail queue to trace problems being encountered by customers.

I have put this article here more as an aide-memoir than anything. If anyone finds it useful, I’d love to hear from you!

Respect the exim queue

One of worst things you could ever do as an admin, when finding a mail queue with hundreds or thousands of frozen or delayed messages in it, is to simply clear the queue. DONT DO THIS! You might just as well not run a mail server at all. The messages you remove in this way will just vanish without a trace – your customers and the people they communicate with won’t have a clue what happened, and your service will soon be viewed as unreliable.

Sadly, many inexperienced admins use the graphical UI to do just that – because it’s easy. However, with just a few simple commands, it can become possible to inspect and manage the queue with much greater elegance.

So, read on to find out how Exim queue management can be done from the command line. Read More…

Linux – Move or copy the contents of one directory into another

Linux TuxThe way Linux expands it’s wildcard, or globbing characters, can be confusing at first and even tasks as simple as copying a few files can sometimes turn into a learning exercise. It is sometimes necessary to move or copy files in bulk from one place to another. There are several methods for doing this in Linux, depending on which tools you have installed (Obviously, tools like rsync or unison might be better in many cases), but for this article we’re just sticking to mastering the cp command.

It’s important to understand how Linux sees the contents of each directory, because there are two special entries within each directory which people often overlook – plus there is the issue of hidden dot files – files whose names start with th dot – e.g.  .htaccess

# ls -a1
./  << This is the current directory hard link
../  << This is the parent directory hard link

Read More…

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

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. Read More…

Bash Script to scan folders and PHP files for bad permissions

This script will run through all Cpanel user account home directories and recursively do the following:

  • check for directories that have the write bit set for group(g) or other(o) – and reset any found to 755 permissions.
  • check for any files with the .php extension and that have any access bits at all allowed for other (o), write/execute bits set for group(g), or execute bit set for user(u) – and reset any found to 640 permissions.

It is quite easy to modify for your own purposes, but these permissions are generally a good starting point when on a server running PHP with the suPHP module (THIS WILL BREAK EVERYTHING IF YOU ARE RUNNING PHP AS DSO). Read More…

Quick Quota Calculator for Cpanel Users

To get an instant view of the diskspace being used by each Cpanel user account home directory, I created this little script. Thanks to Spiral for some pointers on getting the cleaned account list.

CPUSERS=`/bin/ls -- /var/cpanel/users | /bin/grep -v "\.\|cpanel\|root\|mysql\|nobody"`
for CPUSER in $CPUSERS; do
   CPHOME="$(/bin/grep "${CPUSER}:" /etc/passwd | cut -d':' -f6)"
   NEWACC="`/usr/bin/du -m --summarize $CPHOME`"
   S="`printf \"%03d\" $(echo $NEWACC | awk '{print $1}')`"
   let "TOT=$TOT+$(echo $NEWACC | awk '{print $1}')/1"
echo -e $ACCSUM | sort
echo $TOT

As always, do not test in a production environment!

Simple change detector for cpanel template files

OK, in answer to requests, here is a simple bash script to check any file for changes. It could have been done with MD5 hashes, but it’s probably not necessary (overkill).

Just copy the file you want to check and rename it with a different extension (e.g. .check)

Then just run the following script under a crontab entry – probably best a short while after upcp is run.

COMPARE="`diff --brief /usr/local/cpanel/etc/httptemplates/apache2_2/default /usr/local/cpanel/etc/httptemplates/apache2_2/default.check`"
if [ ${#COMPARE} -gt 0 ]; then
   echo "There was a change found in /usr/local/cpanel/etc/httptemplates/apache2_2/default" | /bin/mail -s "Changes in Cpanel Templates Detected `date`" santfiles@mac.com

If the files are the same then diff should return nothing, so we just check for a return value that is greater than 0.

Of course, if you happen to have CSF and LFD installed, then there is no need for any of this – just set up a File watch. (making sure you set LF_DIRWATCH_FILE to a value other than zero to make sure your watched files and directories are actually being watched!)

As always – test test, and test again before using anything here in a production environment!