Subversion pre-commit hook for detection of byte-order marks (BOMs)

Byte-order marks can mess up your code badly. Some of my CodeIgniter PHP code was receiving “headers already sent”, thanks to BOM’s alone. So, our goal is to reject a commit that contains one or more PHP files with BOMs. You can easily change the script to filter other files as well. The script is known to work on Dreamhost.

Enough of talking, here is the pre-commit hook, a bash script, you were desperately searching the Internet for:

#!/bin/bash

REPOS="$1"
TXN="$2"

PHP="/usr/local/bin/php"
SVNLOOK="/usr/bin/svnlook"
AWK="/usr/bin/awk"
GREP="/bin/egrep"
SED="/bin/sed"

CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | $GREP "^[U|A]" | $AWK '{print $2}' | $GREP \.php$`

REGEX=$'\xEF\xBB\xBF'
GREP2="grep -l $REGEX"

for FILE in $CHANGED
do
    MESSAGE=`$SVNLOOK cat -t "$TXN" "$REPOS" "$FILE" | $GREP2`
    if [ $? -eq 0 ]
    then
        echo 1>&2
        echo "***********************************" 1>&2
        echo "Byte order mark error in: $FILE:" 1>&2
        #echo `echo "$MESSAGE" | $SED "s| -| $FILE|g"` 1>&2
        echo "***********************************" 1>&2
        exit 1
    fi
done

Leave a Comment

Make MAMP PRO not ask about passwords on service restart

Change your default MAMP password from ‘root’ to something more useful:
/Applications/MAMP/bin/mysql4/bin/mysqladmin -u root -p password newpassword

Edit the following files:

  • /Applications/MAMP/bin/phpMyAdmin-X.X.X/config.inc.php

    $cfg['Servers'][$i]['password'] = 'newpassword'; // change root to your new password

  • /Applications/MAMP/bin/mamp/index.php

    $link = @mysql_connect(’:/Applications/MAMP/tmp/mysql/mysql.sock’, ‘root’, ‘newpassword‘);

  • /Applications/MAMP/bin/stopMysql.sh

    # /bin/sh
    /Applications/MAMP/bin/mysql4/bin/mysqladmin -u root -pnewpassword –socket=/Applications/MAMP/tmp/mysql/mysql.sock shutdown

Tip taken from this site.

Leave a Comment

Rotating Apache logs with logrotate

It’s pretty easy to set this up on Linux as most distros come with rotatelog functionality.

All I had to do was to create a file /etc/logrotate.d/apache2 with this content:

/usr/local/apache/domlogs/www3.serbiancafe.com.short {
daily
missingok
rotate 5
ifempty
copytruncate
olddir /usr/local/apache/domlogs/oldlogs
}

/usr/local/apache/logs/*_log {
rotate 3
size 100M
copytruncate
}

If plain English:
First log is rotated on a daily basis regardless if the log is there or empty. Last 5 logs are kept in “olddir” directory. And, you don’t have to restart Apache when you use copytruncate directive.

Second group defines a rotation for all files ending with “_log”. They rotate three files when their size is over 100M.

More options can be found in logrotate man page

Leave a Comment

Coda books with custom images for Code Igniter, MySQL, PHP…

Leave a Comment

MAMP: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)

I instaled MAMP and it worked great. However, when I tried to access mysql from the shell I was getting

$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

However, using a full path to MAMP binary worked.
$ /Applications/MAMP/Library/bin/mysql -u root -p

MAMP’s mysql works with another socket file which is located at /Applications/MAMP/tmp/mysql/mysql.sock

All you have to do is:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

UPDATE: As it turns out the above soft link gets erased by the system, so you need to run the command every once in a while. At one point it got too frustrating so I figured out a permanent way to take care of this problem. Open /etc/my.cnf and add/edit these lines:
[mysqld]
socket=/Applications/MAMP/tmp/mysql/mysql.sock

[client]
socket=/Applications/MAMP/tmp/mysql/mysql.sock

Instead of telling MAMP which socket file to use, you are instructing the mysql client to use MAMP’s default socket.

Let me know if this works for you.

Leave a Comment

How to fix /tmp 100% usage problems

This was happening a lot on my main server (RHEL Linux with cPanel). Ever since I chose a cPanel installation, the /tmp directory was often spiking to 100%. The mistery was that I couldn’t list the files to see the cause of those spikes in /tmp usage. Regular ‘ls’ command was showing files, but nothing alarmingly big, and nowhere close to 1G that I had allocated to /tmp.

I first changed a temporary directory for my Web applications. Instead of /tmp I used /tmp-sc, and fixed my applications accordingly. When /tmp goes to 100% at least my applications were able to use a disk cache.

Next, I decided to have MySQL use /tmp-mysql. Created a new directory with proper permissions and added a tmpdir option to mysqld section of /etc/my.cnf

[mysqld]
tmpdir = /tmp-mysql

I restarted MySQL and voila, all my troubles were gone.

The server with 2GB of ram and 2 cores now serves over 1 million PHP pages + many many more static images and the best part is that the load on the server rarely goes above 1.

If you find this tip helpful, let me know.

Comments (1)

Common Linux server commands on a new box

Changing a time zone

Log in as root and type these commands:
mv /etc/localtime /etc/localtime-old
ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime

To be continued

Leave a Comment

Using Windows printers on Mac OS X 10.5 (Leopard)

Some Windows printers are hard to configure. Here’s how you do it:

1. Open “System Preferences”

2. Click “Print and Fax”

3. Click “+” to add a new printer.

4. Click “Advanced” from the tab options. If you don’t see it, than right-click or CTRL-click in the toolbar area (gray on top) and select “Customize Toolbar”. Drag the Advanced icon to the toolbar, click “Done” and click “Advanced”.

5. Be patient. It takes up to a minute for this screen to fill the options. Once done, select type “Windows”, device “Another device” and enter URL as:

smb://username:password@workgroup/machine/printer

My example connection is for user named “serbian” with password “cafe012″. Domain name is “Workgroup”, computer name “silver” and printer was shared by Windows as “Optra”. Fill the rest of the data as you please.

Leave a Comment

Deleting too many files

When you try to delete a big number of files in a directory of thousands using:

rm *

You’ll get an error. Linux complains about “Argument list too long”. Issue this command from a directory where files are located to get rid of them:

find . -exec rm {} \;

If you want to delete a large number of cache files, without deleting them all, the best way is to delete the files that were not recently accessed. Say you want to keep the popular cache hits which were accessed in the last 5 days and remove older files, your command would be:

find . -atime +5 -exec rm {} \;

If you try to use a wildcard when using find, you’ll be limited to the maximum number of pages your argument list can accept, which is a kernel limit. So, to work around this, I created a subdirectory for each cache group so I can use a directory name as the first parameter of find. The following example deletes items from cache which were not accessed in more than 4 days. This Something like this:

find /www/site/ci/system/cache/khcache/ -atime +4 -exec rm {} \;

Previously, I was using a wildcarded ‘find’ which was failing:

find /www/site/ci/system/cache/khcache_* -atime +4 -exec rm {} \;

Don’t forget to fix your cache directory setting in your configuration files.

Leave a Comment

Leopard fixes

Mac OS X 10.5 Leopard is great, but as any other OS, it has it’s growing pains.

Here’s issues I encountered with it:

1. MailActOn doesn’t work with new Mail.app
UPDATE: The Leopard install has been released. Works great.

2. When starting Terminal.app I got this error message:

You are not authorized to run this application. The administrator has set your shell to an illegal value.”

I downloaded iTerm from http://iterm.sourceforge.net/ and issued a command:

sudo chsh -s /bin/bash my_username

That fixed it.

Comments (1)

Older Posts »