Tuesday, November 3, 2015

How to find and kill a stuck process

To find (and kill) a stuck process like a hung deamon in UNIX or Linux, use the following command from the command line:

ps -ef | grep processname (Where processname is the name of a process to be killed)

For an example, if you wanted to find all of the Java processes on the box, use the ps -ef | grep java
command. For all of the apache processes, use ps -ef | grep apache.

You should get back something like this:

user      3258  3238  0 04:31 ?        00:01:45 java -server -Xms512m

The "3258" number after the user running the Java process is the PID number of the running process.

Use this PID number to shut down the process that is running with the kill command. Make sure to enter the right PID number! An example is below:

kill 3258 

If that doesn't kill the process, try running kill -9 2358 (Or whatever your PID is) to forcibly shut down the process. Depending on the version of UNIX or Linux you're using, you might need to run this command as the root user using the sudo command. In that case, the command would look like this:

sudo kill -9 3258