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