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
    Give execute permission to the file (chmod +x
    If you want to kill all java processes type
    ./ java
    Or you can use pgrep and pkill commands if they are available on your system.

    Website Design said...

    Thanks for the useful post.