Wednesday, September 7, 2016

find and kill a unix process

There are many instances when we have to find and kill a unix process.

Following is the command used to kill a process forcefully:

      kill -9 <process-id>

But we are not always aware of the process Id all the time . We can follow either of following two steps to find the processId :

1)
     pidof can be used to find the process id for process you know the description of
Usage :
   
     pidof /root/endeca/PlatformServices/11.0.0/j2sdk/bin/java

where /root/endeca/PlatformServices/11.0.0/j2sdk/bin/java exists in description of the pid

2) Use ps command to list all the processes and find out the one we need to delete. The process id is the highlighted number below . the result is returned on execution of ps command :

    <USER>      15888  2130  2 17:31 pts/0    00:00:03 <ENDECA-HOME>/PlatformServices/11.0.0/bin/forge -vw --javaClasspath <ENDECA-HOME>/CAS/latest/lib/recordstore-forge-adapter/recordstore-forge-adapter-11.0.0.jar:./config/lib/java/spring-2.5.6.jar:./config/lib/java/eacToolkit-11.0.0.17592292.jar:./config/lib/java/spring-delegate-adapter-1.0.1.jar:./config/script:. --javaArgument -DENDECA_PROJECT_NAME=<APP-NAME> --javaArgument -Djava.util.logging.config.file=./config/script/logging.properties --logDir <ENDECA-HOME>/apps/<APP-NAME>/./logs/forges/Forge --outputPrefix <APP-NAME> --inputDir <ENDECA-HOME>/apps/<APP-NAME>/data/processing --outputDir <ENDECA-HOME>/apps/<APP-NAME>/data/forge_output --stateDir <ENDECA-HOME>/apps/<APP-NAME>/data/state --numPartitions 1 --tmpDir <ENDECA-HOME>/apps/<APP-NAME>/data/temp -o <ENDECA-HOME>/apps/<APP-NAME>/logs/forges/Forge/Forge.log <ENDECA-HOME>/apps/<APP-NAME>/data/processing/pipeline.epx

Here 15888 is the process ID.

Once we have process Id we can kill it using kill command :

Usage:

      kill -9 15888

list all active processes in unix using ps command

Unix ps command is used to list down  the currently running process in the system.

There are various flags ps is used with . Most commonly used flag with ps are -e and -f

-e : This option is used to list all the threads

-f : This option is used to list the complete format of the process.

Usage :

          ps -ef

This might return a huge list of processes , the ideal way is if you know the terms in the process format like "java" and grep on that .

Usage :

          ps -ef | grep java