Quick text search in files on the commandline

Posted: August 29th, 2009 | Filed under: Geekstuff, Uncategorized | Tags: , | No Comments »

Today i wrote a simple bash script to simplify my often used greps over text files in the terminal. Here it is:

#!/bin/bash
if [[ 1 -eq $# ]]; then
    find . -type f -exec grep -Hs "$1" {} \;
fi
if [[ 2 -eq $# ]]; then
    find . -name "*.$1" -exec grep -Hs "$2" {} \;
fi
if [[ 3 -eq $# ]]; then
    find "$1" -name "*.$2" -exec grep -Hs "$3" {} \;
fi

So, you can call the script with 1-3 arguments.

  • If you give one, it is assumed that you want to search the current directory (and all its subdirectories) for the given text.
  • If you give one more argument, you can search all files with a specific file extension and
  • if you choose to give three arguments you can search a given directory for all files with a given extension.

I named the script "findtext", chmod’ed it executable and put it somewhere on the $PATH.


On Sale: Apple PowerMac G5 Dual 1,8 Ghz

Posted: July 19th, 2009 | Filed under: Geekstuff, advertising | Tags: , | No Comments »

I’m selling my beloved Apple G5 Workhorse due to a switch to a new Macbook Pro. If anyone is interested in buying it, please contact me!

Apple PowerMac G5 Case

Apple PowerMac G5

The overall condition of the machine is excellent. No hangs, no freezes, no problems. I’m non-smoker, so it looks almost on it’s first day.  – There are even still some original protective foils on it. Here are the specs:

  • Apple PowerMac with two G5 PowerPC processors, each clocked at 1,8 GHz
  • 4 GB RAM main memory
  • Two Serial ATA harddisks builtin: 232 GB and 150 GB capacity
  • Sony DVD Superdrive DW-U10A – DVD and CD burning was tested recently and fully functional
  • Graphics card: ATI Radeon 9600 Pro 64 MB RAM with DVI and ADC connectors. I’ll add an ADC to DVI and a DVI to Video adapter to the package, so you’re able to connect up to two displays with DVI or one display and one TV to the Mac.
  • Bluetooth available through an included USB Bluetooth adapter
  • Original Apple Keyboard and Mouse were recently cleaned by me and are also in a excellent condition
  • Original Mac OSX 10.3 included and installed
  • Original iLife ‘04 included and installed
  • Setup Discs are in excellent condition

Price is subject to negotiation, but i thought about somewhat around 400 to 450 Euros.


Instant Java web deployment with Jetty and Maven

Posted: June 30th, 2009 | Filed under: Geekstuff | Tags: , , , | No Comments »

Having short roundtrip times between coding in the IDE and having it deployed in the web container is the key for efficient development. So here is how you can setup it with Maven and Jetty including Debugging in the IDE. – I used Eclipse, but any other modern IDE should do the work.

Configure your web project’s pom.xml

The following configures the Jetty Maven plugin to serve your project at port 8080 and scan every 5 seconds for changes of your .class files. – So if you have your IDE configured to automatically rebuild changed classes and it puts them into the Maven projects target directory, you can see changes instantly by just hitting the browser’s reload button.

    <build>
        <finalName>myproject</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8080</port>
                        </connector>
                    </connectors>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
    </build>

You can test it on the commandline by going into your project and enter mvn jetty:run. Then your webapp should be available at http://localhost:8080/myproject. The log output goes into console. Change something in your project, wait 5 seconds and hit the reload button.

Debugging the webapp

Debugging the webapp deployed in Jetty is quite easy. Set the MAVEN_OPTS environment variable to include debugging before executing the Maven Jetty plugin:

export MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n"

The Java process now is listening on port 4000 and you can easily connect your IDE to it (in Eclipse the debug type is called Remote Java application).

Of course you can simplify the setup by executing the Maven Jetty plugin from your IDE. In the Debug configuration you add the setting of the MAVEN_OPTS variable and you’re ready to go in just one click.


Troubleshooting Java cpu utilization

Posted: April 3rd, 2009 | Filed under: Geekstuff | Tags: , | No Comments »

I had a stateless Java service running in a Glassfish that shortly after the first requests came in, began to consume a whole processor core – even when no more requests come in. The only way to stop it, was to restart the JVM. Needless to say, this is no fun for the server admin boys ;) As there are a lot of threads hanging around in the server’s JVM (and they are not always obvious in e.g. VisualVM), here is a way how to identify it without any doubt.

1. Get the Java process owning the thread

top

and look for the process id (pid) of the beast.

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
5083 root      20   0 1690m 1.2g 5696 S 94.4 32.2   1485:59 java
4823 root      20   0 1152m 202m 3604 S  1.3  5.4  21:44.97 java
4349 root      20   0 17060  656  492 S  1.0  0.0   1:50.09 vmware-guestd
4724 proxy     20   0 39420 5016 1192 R  0.3  0.1   5:58.29 squid3

2. Get the Java Thread that’s responsible for the cpu consumption

ps -fLp <pid>

A huge list is printed where you can see all the threads with their respective cpu consumption.

root      5083  4970  5092  0  130 Feb19 ?        00:00:00 /usr/lib/jvm/java-6-sun-1.6.0.11/jre/../bin/java -Dcom.sun.aas.instanceRoot=
root      5083  4970  5093  0  130 Feb19 ?        00:00:31 /usr/lib/jvm/java-6-sun-1.6.0.11/jre/../bin/java -Dcom.sun.aas.instanceRoot=
root      5083  4970  5094 92  130 Feb19 ?        23:42:32 /usr/lib/jvm/java-6-sun-1.6.0.11/jre/../bin/java -Dcom.sun.aas.instanceRoot=
root      5083  4970  5095  0  130 Feb19 ?        00:00:00 /usr/lib/jvm/java-6-sun-1.6.0.11/jre/../bin/java -Dcom.sun.aas.instanceRoot=
root      5083  4970  5096  0  130 Feb19 ?        00:02:01 /usr/lib/jvm/java-6-sun-1.6.0.11/jre/../bin/java -Dcom.sun.aas.instanceRoot=

Take the thread id (LWP) of the one which takes all the cpu power (here its 5094 using 92%) and convert it into an hexadecimal

5094 decimal = 13e6 hexadecimal

3. Now you can make a thread dump

… to get the thread name (and hopefully some information what it’s going on there):

kill -QUIT <pid>

Look in the catalina.out (Tomcat) or jvm.log (Glassfish) logfile to see all the processes. You can also get the threaddump simply by invoking

jstack <pid>

In the dump you can simply search for the hexadecimal version of your  thread id (it’s prefixed with “0x” and the hexadecimals are lowercase).

In my case the thread found was:

$ jstack 5083 | grep -C 5 0x13e6

"Low Memory Detector" daemon prio=10 tid=0x00000000401c8c00 nid=0x13e7 runnable [0x0000000000000000..0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"CompilerThread1" daemon prio=10 tid=0x00000000401c6800 nid=0x13e6 runnable [0x0000000000000000..0x000000004231c470]
   java.lang.Thread.State: RUNNABLE

"CompilerThread0" daemon prio=10 tid=0x00000000401c2c00 nid=0x13e5 waiting on condition [0x0000000000000000..0x000000004221b3e0]
   java.lang.Thread.State: RUNNABLE

So, its the HotSpot’s CompilerThread who eats up my processing power.

4. Debugging the evil monster

Knowing the responsible one i can enable logging of the HotSpot’s current compile tasks with the JVM option

-XX:+PrintCompilation

If something is compiled over and over again, one can disable distinct methods with

-XX:CompileCommand=exclude,net/sprd/foo/MyClass,someMethod    # direct JVM parameter
-XX:CompileCommandFile=/my/canned/command/file                # Exlusion through a configfile

The configuration file has the following scheme:

exclude         net/sprd/foo/MyClass      someMethod

Console output should acknowledge the setting then with

### Excluding compile:  net.sprd.foo.MyClass::someMethod

Mounting zeroconf (Bonjour) services between different subnets

Posted: March 22nd, 2009 | Filed under: Geekstuff | Tags: , , , , | No Comments »

What if you’re on the road and your favorite service of your local network isn’t available: the music share?! Well, here is a short tutorial which leads you through the two basic steps:

  • set up an SSH tunnel to the network (if you can’t connect to the musicserver directly, like in some firewall’ed setups)
  • Anounce the zeroconf service in your current network

Well, but this can also be a guideline for any network service that is limited to your subnet. The following assumes that you’re using the DAAP protocol for music sharing (implemented in iTunes and the Firefly Media Server) and your local machine is a Mac – on Linux it is pretty much the same work, maybe even easier as the mDNSResponderPosix software is already installed or easily available in your distribution.

So, first task: SSH tunnel. This is the easy part:

ssh -L 3689:musicservers_ip:3689 -l login yourgatewayhost

The DAAP port is 3689 and the machine yourgatewayhost serves as a hop between you and your musicserver. – This is not needed if you can connect directly to the musicserver (but i wouldn’t make the service available to the public internet, dude!).

So, as the service is available on your local machine port 3689, now you have to announce it via Bonjour to be able to connect to it. The nifty small cli tool for this is mDNSResponderPosix – available on the Apple opensource site. Download and unpack it and then do the following:

cd mDNSPosix/
make os=panther
sudo cp build/prod/mDNSProxyResponderPosix /usr/local/bin/

Now you can announce your music share with

/usr/local/bin/mDNSProxyResponderPosix 127.0.0.1 musicserver "rockin share" "_daap._tcp" 3689

Just call the mDNSProxyResponderPosix app with the –help commandline arguments if you’re interested in the arguments meanings.

Of course with the mDNSProxyResponderPosix app you’re able to anounce a whole world of services in your network, which the MacOSX Finder and some Linux Filemanagers then can just pick up and make it available to you in their GUI.


Good to know …

Posted: November 5th, 2008 | Filed under: Blahblah | Tags: , | No Comments »

… that there are still people who appreciate good straight music like this fellow.

If you also want to stay tuned in good music have a look over at cover.norocketscience.net.