Finding and Killing Processes

A fundamental skill to have within the terminal is being able to kill processes. The scope of errors a rogue process can cause is immense. Recently while running MongoDB I would get an error

errno:48 Address already in use for socket:

Permission denied Is a mongod instance already running?, terminating

Even though I didn't have any terminal windows or tabs open running mongo, it would refuse the connection because there was a rogue process associated with 'mongod'. I found it handy to learn the 'ps' command, which records a snapshot of the current running processes. Three common options to refine the search are shown below.

a = show processes for all users
u = display the process's user/owner
x = also show processes not attached to a terminal

Then you can use the pipe to search by a program name, typically getting you exactly what you are looking for.

ps aux | grep mongo 

root            77397   0.4  0.8  3209100  68420 s000  S+   11:17AM   2:17.38 mongod

root            77396   0.0  0.0  2469088    156 s000  S+   11:17AM   0:00.02 sudo mongod

robhaj          24016   0.0  0.0  2423356    200 s001  R+    9:46AM   0:00.00

The command returned three processes, two from root and one on the user. The number to the right of that (i.e 77397, 77396, or 24016) are the PID, or process ID's. Using 'sudo kill #PID' where you pass in the actual PID, you can kill an unwanted or rogue processes.