? Earlier 11 items total Later ?

On this page:?

Measure the daily number of E-mail messages in a mailbox

This snippet written in bash with calls to perl from the command line measures the number of E-mail messages sent to a mailbox per calendrical day.

#!/bin/bash
grep -h '^Date:' * |
    perl -pe 's!^Date: !!' |
    perl -pe 's!^\w\w\w, !!' |
    perl -pe 's{\d{2}:\d{2}:\d{2}.*$}{}' |
    perl -pe 's!^\s+!!' |
    perl -pae '$_=sprintf("%.2d-%s-%s\n", @F)' |
    sort | uniq -c | sort -n


I used perl for some places where sed would have been more suitable because I find the sed regexp syntax confusing. :-)

yet another lighttpd restart script

#!/bin/sh

USERNAME=username
LIGHTTPD_CONF=/home/$USERNAME/service/lighttpd.conf
PIDFILE=/home/$USERNAME/service/logs/lighttpd.pid
HTTPD=/home/$USERNAME/lighttpd/sbin/lighttpd

PID=0
if [ -e $PIDFILE ]; then
    PID=`cat $PIDFILE`
    if [ "x" == "x$PID" ]; then
        PID=0
    fi
fi

case "$1" in
    start)
        if [ 0 -ne $PID ]; then
            running=`ps --pid $PID | grep $PID`
            if [ $running ]; then
                echo "lighttpd is already running"
                exit 1
            fi
            rm $PIDFILE
            PID=0
        fi
        $HTTPD -f $LIGHTTPD_CONF
        ;;
    stop)
        if [ 0 -eq $PID ]; then
            echo "lighttpd was not running"
            exit 1
        fi
        kill $PID
        tries=""
        while [ -e $PIDFILE ]; do
            if [ "x$tries" == "x.........." ]; then
                break
            fi
            sleep 2
            tries=".$tries"
        done
        if [ -e $PIDFILE ]; then
            echo "lighttpd did not go gentle into that good night, murdering"
            kill -9 $PID
            rm $PIDFILE
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        if [ 0 -eq $PID ]; then
            echo "lighttpd was not running"
        fi
        kill -HUP $PID
        ;;
    *)
        echo "Usage: "`basename $0`" (start|stop|restart|reload)"
        exit 1
        ;;
esac

Cron zombie killer

This searches for and destroys the zombie processes that linger (and block I/O for an indefinite amount of time) after cron starts up on a shared server:

for each in `ps jauxww | grep Z | grep -v PID | awk '{print $3}'`; do for every in `ps auxw | grep $each | grep cron | awk '{print $2}'`; do kill -9 $every; done; done

SLOC of php source code

This shell snippet counts all lines in all .php files (in and beneath the current path ".") that are non-blank and not comments (yes, it also discards multiline comments).

find . -name '*.php' | xargs cat | sed -re ':top /\/\*.*\*\// { s/\/\*.*\*\///g ; t top }; /\/\*/ { N ; b top }' | awk '$0 !~ /^[\t[:space:]]*($|(\/\/)|(#))/' | wc -l

Recursively remove all .svn directories

find . -name .svn -print0 | xargs -0 rm -rf


Update: Thankyou iburrell

Simple bash calculator

Put the following little snippet in your .bashrc/.bash_profile and you'll have a handy little calculator:

? () { echo "$*" | bc -l; }


Use like this:

bash$ ? 2*2
4


It should be easily modifiable for other shells/calculators.

Via: http://rentzsch.com/rock/longhand

Remove all but X most recent files in directory

Remove all but 5 (or whatever number) of the most recent files in a directory. Great if you have a directory of log files and you only want to keep a few of the recent ones.
rm `ls -t | awk 'NR>5'`

Redirect all output to a single file

Shell commands often output to both standard output and standard error. If you want to send *all* of this to a log file, use the following.

your_shell_script >> /path/to/logfile 2>&1


The 2>&1 bit is the part that redirects standard error to standard out, allowing you to capture both.

How to flush the local DNS cache on Mac OS X

If you want to add a virtualhost on your Mac OS X box without having to wait around for ages, then the easiest way to do so is to shove a line into /etc/hosts and flush the dnscache. Here's a friendly bash function to throw into your .bashrc:

function edithosts {
        if [ -x "`which $EDITOR`" ] || [ -x "`which $1`" ]
        then
                if [ -x "`which $EDITOR`" ]
                then
                        export TEMP_EDIT="`which $EDITOR`"
                else
                        export TEMP_EDIT="`which $1`"
                fi
                echo "* Using ${TEMP_EDIT} as editor"
                $TEMP_EDIT /etc/hosts && echo "* Successfully edited /etc/hosts"
                lookupd -flushcache && echo "* Flushed local DNS cache"
        else
                echo "Usage: edithosts [editor]"
                echo "(The editor is optional, and defaults to \$EDITOR)"
        fi
        unset TEMP_EDIT
}


More simply, you can just flush the DNS cache manually with:

lookupd -flushcache

Bash function to copy SSH DSA public key to a new server

Or, "How to login over SSH without a password".

First of all, you'll need to have generated an SSH public DSA key using the following commands:

cd ~ && ssh-keygen -t dsa


Then, you can add the following bash function to your .bashrc (or similar) and just type 'scpdsa user@hostname' to add your public key to your 'authorized_keys' file on the server.

function scpdsa {
        if [ ! -n "$1" ];
        then
                echo "!! You need to enter a hostname in order to send your public key !!" 
        else
                echo "* Copying SSH public key to server..." 
                scp ~/.ssh/id_dsa.pub $1:~/id_dsa_temp.pub
                echo "* Adding SSH public key to 'authorized_keys' on server..."
                ssh $1 'cat ~/id_dsa_temp.pub >> ~/.ssh/authorized_keys'
                echo "* Removing temporary files from server..."
                ssh $1 'rm ~/id_dsa_temp.pub'
                echo "* All done!"
        fi
}

? Earlier 11 items total Later ?