Thursday, December 6, 2007

Kill A Process By Name

My favorite shell command is kill. Don't worry I'm not a serial killer.
If you know the process id and if you have enough permissions you can kill any process you want . It is easier to kill a process by name. Here is the command.

kill -9 `ps -ef | grep $1 | grep -v grep | awk '{print $2}'`


kill -9 processId sends SIGKILL signal to specified process. You can use a list of process ids as kill command argument. kill -9 pid1 pid2 pid3
  • ps -ef|grep $1
    Lists process that matches $1
  • ps -ef | grep $1 | grep -v grep
    List process that matches $1 except the process that runs grep command
  • ps -ef | grep $1 | grep -v grep | awk '{print $2}
    Lists only process ids of processes that matches $1 except the process that runs grep command.

    awk '{print $2}' lists the second column of the
    ps -ef | grep $1 | grep -v grep 
    command output.

    Save kill -9 `ps -ef | grep $1 | grep -v grep | awk '{print $2}'` in a file called myKill.sh
    Give execute permission to the myKill.sh file (chmod +x myKill.sh)
    If you want to kill all java processes type
    ./myKill.sh java
    Or you can use pgrep and pkill commands if they are available on your system.
  • Tuesday, December 4, 2007

    Watch multiple log files with "tail"

    Log files are the first place i look while debugging an application. To see the logs in real-time I am using the utility command "tail" with the "-f" option.

    tail -f filename

    When there are multiple log files to watch out, I used to open different xterm console to tail each file.But today I learned that "tail" can watch multiple files for changes. Example command and its output on my PC (Ubuntu 7.10).


    tail -f logs/catalina.out InaWs.log InaWsUserActions.log

    ==> logs/catalina.out <==
    [2007-12-04 09:39:18,746] INFO(ContextManager.java.initialize:56) - Context [vpn] initialized.
    [2007-12-04 09:39:18,760] INFO(ContextManager.java.initialize:56) - Context [prepaid] initialized.
    [2007-12-04 09:39:18,761] INFO(InaWsImpl.java.initialize:116) - Using auhentication factory : com.oksijen.inox.common.ws.auth.PlaintextAuthenticationFactory
    Dec 4, 2007 9:39:19 AM com.sun.xml.ws.transport.http.servlet.WSServletDelegate
    INFO: WSSERVLET14: JAX-WS servlet initializing

    ==> InaWs.log <==
    [2007-12-04 09:39:18,743] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (InaWsContext.java.addAction:41) - Adding action. actionName[echoVpn]...
    [2007-12-04 09:39:18,744] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (C3p0Impl.java.initialize:53) - Initializing C3P0 connection pool....
    [2007-12-04 09:39:18,757] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (InaWsContext.java.addAction:41) - Adding action. actionName[echoPrepaid]...
    [2007-12-04 09:39:18,758] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (C3p0Impl.java.initialize:53) - Initializing C3P0 connection pool....

    ==> InaWsUserActions.log <==
    20071204093501054 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root prepaid echoPrepaid [test]
    20071204093501054 RES 117787e671a35c5c03e072efbd53bad3cea0ae49 1
    20071204093520876 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root vpn echoVpn [test]
    20071204093520876 RES 117787e671a35c5c03e072efbd53bad3cea0ae49 1
    20071204093530380 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root mpbx echoMpbx []
    20071204093546634 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root mpbx listAgents []
    20071204093549223 RES 117787e671a35c5c03e072efbd53bad3cea0ae49 100101

    Monday, November 12, 2007

    My favorite shell commands

    The below one line shell script calculates the number of executions of commands and sorts the result.

    history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -rg

    Here is the result of this script on my Ubuntu.

    66 ls
    40 cd
    32 sudo
    32 mvn
    31 ps
    29 clear
    26 more
    22 vi
    19 tail
    18 kill
    16 ssh
    13 rm
    13 eclipse
    11 tomcat_start.sh
    10 ll
    10 find
    9 locate
    9 findJar
    6 ifconfig
    5 ln

    Most frequently used command is "ls". I was surprised to see "mvn" in the 4th place. As you can see I am developing (eclipse), testing-packaging (mvn) and deploying (tomcat_start.sh) web applications written in java these days. I have also "kill"ed a serious number of processes :)

    Try this script to see what commands you use frequently.

    Thursday, October 18, 2007

    Sınır Ötesi Operasyon

    Son günlerde gündemi meşgul eden en önemli haber TBMM'nin hükümete sınır ötesi harekat için yetki vermesi. Sınır ötesi operasyon gerekli mi gereksiz mi tartışmaları sürerken gerçekler gözden kaçırılıyor. TBMM'de bizi temsil eden millet vekilleri arasında PKK'yı bölücü teror örgütü olarak tanımayan millet vekilleri var. Durum böyleyken bizim operasyonu sınır ötesinde değil öncelikle kendi millet meclisimiz içinde yapmamız gerekir. Türkiye Cumhuriyeti vatandaşlarını, devletin bölünmez bütünlüğüne zarar vermek için acımasızca öldüren, binlerce çocuğu anasız babasız bırakan eli kanlı bu terör örgütünü "TERÖR ÖRGÜTÜ" olarak adlandıramayan millet vekilleri meclis çatısı altında nasıl barınır aklım almıyor. Demokrasi böyle birşey olsa gerek.

    Blogged with Flock

    Thursday, October 4, 2007

    Volatile Variables In JAVA



    Java Memory model defines the possible rules for threads and the expected behavior of multi-threaded programs so that programmers can design their programs accordingly. JSR-133 describes the semantics of threads, locks, volatile variables and data races.

    Volatile variables are treated different from other variables. When you mark a variable as volatile, this warns the compiler to get fresh copies of the variable rather than caching the variable in the registers.

    Volatile variables can be used to store shared variables at a lower cost than that of synchronization, but they have limitations. While writes to volatile variables are guaranteed to be immediately visible to other threads (because JVM does not allow threads to cache volatile variables), there is no way to render a read-modify-write sequence of operations atomic, meaning, for example, that a volatile variable cannot be used to reliably implement a mutex (mutual exclusion lock) or a counter.

    Let's demonstrate this with a counter example. In the example a volatile counter variable will be incremented by multiple threads simultaneously. Below is the main class that holds the volatile variable and starts the threads.



    01 package com.oksijen.concurrency.volatiletest;
    02 
    03 import java.util.concurrent.CountDownLatch;
    04 
    05 public class VolatileTest {
    06 
    07     public static volatile long counter = 0L;
    08     private CountDownLatch allThreadsAreDone = null;;
    09     private CountDownLatch allThreadsAreReady = null;
    10     
    11     public static void main(String[] args) {
    12         VolatileTest test = new VolatileTest();
    13         int numberOfThreads = 10;
    14         int incrementCountPerThread = 10000;
    15         CountDownLatch countDownLatch1 = new CountDownLatch(numberOfThreads);
    16         test.setAllThreadsAreDone(countDownLatch1);
    17         CountDownLatch countDownLatch2 = new CountDownLatch(numberOfThreads);
    18         test.setAllThreadsAreReady(countDownLatch2);
    19         test.start(numberOfThreads, incrementCountPerThread);
    20         test.waitForThreadTermination();
    21         System.out.println("Expected counter value : " + numberOfThreads * incrementCountPerThread);
    22         System.out.println("Result : " + VolatileTest.counter);
    23     }
    24     /**
    25      * prints out result after all threads finish execution
    26      */
    27     private void waitForThreadTermination() {
    28         try {
    29             System.out.println(Thread.currentThread().getName() " waiting for other threads...");
    30             this.allThreadsAreDone.await();
    31             System.out.println(Thread.currentThread().getName() " threads finished their work.");
    32         catch (InterruptedException e) {
    33             e.printStackTrace();
    34         }
    35     }
    36     /**
    37      @param numberOfThreads
    38      @param incrementCount
    39      */
    40     private void start(int numberOfThreads, int incrementCount) {
    41         for (int i=0; i<numberOfThreads; i++) {
    42             MyThread thread = new MyThread(incrementCount, this.allThreadsAreDone, this.allThreadsAreReady);
    43             thread.setName("MyThread_"+i);
    44             thread.start();
    45             this.allThreadsAreReady.countDown();
    46         }
    47     }
    48     /**
    49      * synchronized counter increment.
    50      */
    51     public static synchronized void incrementCounter(){
    52         counter++;
    53     }
    54     /**
    55      @param allThreadsAreDone the allThreadsAreDone to set
    56      */
    57     public void setAllThreadsAreDone(CountDownLatch allThreadsAreDone) {
    58         this.allThreadsAreDone = allThreadsAreDone;
    59     }
    60     /**
    61      @param allThreadsAreReady the allThreadsAreReady to set
    62      */
    63     public void setAllThreadsAreReady(CountDownLatch allThreadsAreReady) {
    64         this.allThreadsAreReady = allThreadsAreReady;
    65     }
    66     
    67 }


    Here is our example thread source code:



    01 package com.oksijen.concurrency.volatiletest;
    02 
    03 import java.util.concurrent.CountDownLatch;
    04 
    05 public class MyThread extends Thread {
    06 
    07     private int incrementCount = 0;    
    08     private CountDownLatch done = null;
    09     private CountDownLatch ready = null;
    10     
    11     public MyThread(int incrementCount, CountDownLatch done, CountDownLatch ready){
    12         this.incrementCount = incrementCount;
    13         this.done = done;
    14         this.ready = ready;
    15     }
    16     
    17     public void run() {
    18         try {
    19             System.out.println(Thread.currentThread().getName() " started.");
    20             this.ready.await();
    21             for (int i=0; i<this.incrementCount; i++) {
    22                 VolatileTest.counter++;
    23                 /*
    24                  * if we want to increment the counter synchronously we use the
    25                  * sync incrementCounter() method.
    26                  
    27                  * VolatileTest.incrementCounter();
    28                  */  
    29             }
    30             System.out.println(Thread.currentThread().getName() " ended.");
    31             this.done.countDown();            
    32         catch (InterruptedException e) {
    33             e.printStackTrace();
    34         }
    35     }
    36 }


    I used jdk1.5.0_12 to run this application. I used 10 threads, each increments the variable 10000 times. The result is as expected. Volatile keyword does not guarantie mutual exclusion. Here is the result:

    Expected counter value : 100000
    Result : 85751
    As a result: Volatile variables are best for use when there is only one thread that makes writes (updates volatile variable) and N threads that make reads. Reader threads get the most recent value of the volatile parameter when they perform a read.

    Friday, September 14, 2007

    Generating unique strings with JAVA

    If you need randomly generated Strings in your java code you can use the below functions.


    01  public String generateRandomString(String s) {
    02   try {
    03    SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
    04    String randomNum = new Integer(prng.nextInt()).toString();
    05    randomNum += s;
    06    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    07    byte[] result = sha.digest(randomNum.getBytes());
    08    return hexEncode(result);
    09   catch (NoSuchAlgorithmException e) {
    10    return  System.currentTimeMillis()+"_"+username;
    11   }
    12  }


    The classical hexEncode method:

    01 protected String hexEncode(byte[] aInput) {
    02   StringBuffer result = new StringBuffer();
    03   char[] digits = '0''1''2''3''4''5''6''7''8''9',
    04     'a''b''c''d''e''f' };
    05   for (int idx = 0; idx < aInput.length; ++idx) {
    06    byte b = aInput[idx];
    07    result.append(digits[(b & 0xf0>> 4]);
    08    result.append(digits[b & 0x0f]);
    09   }
    10   return result.toString();
    11  }


    Suppose that you need to generate unique random session ids for your logged in users. You can use the above function as follows :

    String sessionId = generateRandomString(username);


    10 consecutive calls to generateRandomString("ilkinulas") generates the following strings:

    38aca43c835888bc38fbb2d431f537489d637427
    8c9fec7eb1309cd60a9548a11b16a30c2cf277a4
    d495e83405704d38bfdf1bc3e2a38b31a5b52243
    e3f15b900a3b80936596dc4471c0e42889dbf00c
    83a4c869593af633a97b3b68433fac19bac7937d
    f7573e8fdb4ecf6f58062570b66943b08d87fab8
    c19bfdf75abbafbb5d9ca0ce37b90e97f08fccfa
    41f0d9cd01ff814f8879bbff4b06642fc01a4624
    aae1a792389239ca8db35a1ff9b16856c9921845
    c2c9041ab5039b38c79a125c57a10cc6a3eabad9

    Friday, September 7, 2007

    The power off shell

    Linux shell is a powerful tool for developers. Basic commands such as find, grep, diff can be combined to make our lives easier. Suppose that we are working on a project that uses CVS as source code control system, and there are plenty of versions of the same project on CVS. How can you find out the changed java files between version V1 and version V2? The answer is simple: use cvs and diff commands.

    First, checkout the two versions of the project in to two different directories. Create a directory named v1. Checkout a version of the source into directory v1.

    mkdir v1
    cd v1
    cvs -d :pserver:ilkinba@192.168.43.12:/usr/local/cvsroot/ina co -rICU_01_03_01 common/InoxCommonUtil


    Create a directory named v2. Checkout a version of the source into directory v2.

    mkdir v2
    cd v2
    cvs -d :pserver:ilkinba@192.168.43.12:/usr/local/cvsroot/ina co -rICU_01_03_02 common/InoxCommonUtil

    Now it is time to use diff command.

    diff -r -q v1/ v2/

    This command outputs all the files that differ in directory v1 and v2.
    "r" option is used to tell diff command to work recursively.
    "q" option is used to tell diff command to work briefly. diff will only write the names of the different files.
    Use grep to filter java source files.

    diff -r -q v1/ v2/ | grep .java

    After detecting the changed java source files you can use diff to see what has been changed in the java source files.

    $ diff -r -q v1 v2 |grep .java
    Files v1/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java and v2/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java differ

    $ diff -y --suppress-common-lines v1/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java v2/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java

    The result of the above command will be something like:

    && st[i].indexOf(label) >= 0) | && st[i].indexOf(fileName) >=

    "y" option tells diff to output results in two columns(side by side)
    "--suppress-common-lines" option is used to display only changed lines. Common lines will not be displayed.
    Thats all.

    Tuesday, September 4, 2007

    Search java class files in jars.

    This script helps you find the jar file which contains specified java class file.

    Usage is simple :

    ./findClass /home/ilkinulas/lib javax.xml.ws.WebFault

    This command searchs all the jars in directory /home/ilkinulas/lib and lists the jars that contain javax.xml.ws.WebFault java class.

    #!/bin/bash

    START_DIR=$1;
    KEYWORD=$2;

    for i in `find $START_DIR -name *.jar`
    do
    (jar tf $i | grep -q $KEYWORD) && echo $i;
    done