Archive for category linux

Some tips on java.lang.OutOfMemoryError: unable to create new native thread

Thats right, the dreaded, java.lang.OutOfMemoryError: unable to create new native thread. I fixed one of these the other day. And I will present to you how I did it. The cause for such errors are usually Thread Leaks. A Thread Leak is where some ass hat is creating threads which never die. Another reason for such errors is the limits an Operating System can place on the number of threads per process. If hit this limit, you would ge the same error.
Read the rest of this entry »

Simulating dropped packets (aka crappy internets) with iptables

Disclaimer #1

I am not an iptables expert. This tip will work best on your local linux development box where you probably have no iptables rules setup. Do not play around with this shit on production machines, unless you know what you are doing. Speak with your friendly sysadmins before doing this on any machine for which they feel responsible.

Disclaimer #2

According to this tip, do not drop more than 14% of the packets otherwise you will cause all tcp sockets to stall.

Introduction

Why would you want to do this? sometimes people (aka customers) might complain that your super-duper application that they use via the Internets (aka a series of tubes) is slow. One reason could be a dodgy internet connection and/or packet loss. See this great article about how packet-loss affects web applications and how to drop packets with a microwave oven.

And now finally…

To simulate a dropped packets with iptables, you can use the following commands (as root):

# for randomly dropping 10% of incoming packets:
iptables -A INPUT -m statistic --mode random --probability 0.1 -j DROP

# and for dropping 10% of outgoing packets:
iptables -A OUTPUT -m statistic --mode random --probability 0.1 -j DROP

Once done, you can use the following for removing these packet-drops:

# for the incoming packets:
iptables -D INPUT -m statistic --mode random --probability 0.1 -j DROP

# and for the outgoing packets
iptables -D OUTPUT -m statistic --mode random --probability 0.1 -j DROP

Auto Install the SUN JDK on Linux

Sometimes (yes it does happen), you need to install the JDK from SUN automatically (without having to answer yes/no to the license). Why? For example in a Kick Start script to auto-install the JDK.

And so to do this, here a bash & perl script for this exact purpose. This script works only with the .bin install package that you download from SUN’s website.

#!/bin/bash

jdkBinFile=$1

echo $1

# just spew out the agreement
perl -p -i -e 's/^more/cat/g' $1

# set the 'agreed' value for jdk 1.5
perl -p -i -e 's/\s+agreed=$/agreed=1/g' $1

# set the agree value for jdk 1.6
perl -p -i -e 's/`agree`/yes/g' $1

# do not call the register_jdk method
perl -p -i -e 's/\s+register_JDK/#register_JDK/g' $1

# and now run the installation
bash $1

How to

Create a new script file called autoinstall.sh containing the above script. and run it as follows:

$ cd /install/to/directory
$ autoinstall.sh /my/downloads/jdk-6u16-linux-x64.bin

scp2here: scp command to here

I usually need to scp stuff from/over-to servers on a regular basis. And here is a function in .bashrc that I use:

#function to get the scp path to here
function scp2here
{
    user=`whoami`
    server=`hostname`
    pathToHere=`pwd`

     output=$user"@"$server":"$pathToHere

    if [ ! -z "$1" ]
    then
        output=$output"/"$1
    fi

    echo $output
}

This function prints out the scp command argument to copy a file/directory to a location.

Examples

Then to copy something to a target server where I have scp2here, I can cd to the target directory and do this:

srasul@some.cool.host ~ $ scp2here
srasul@some.cool.host.com:/home/srasul
...
srasul@another.cooler.host ~ $ scp bigassfile.txt srasul@some.cool.host.com:/home/srasul

Or if I want to copy a file from a location that has scp2here:

srasul@some.cool.host ~ $ scp2here somefile.txt
srasul@some.cool.host.com:/home/srasul/somefile.txt
...
srasul@another.cooler.host ~ $ scp srasul@some.cool.host.com:/home/srasul/somefile.txt .