[Unix] Kill a long running process in Unix

How To - Kill a long running process in Unix.

Unix process run in background and keep on performing the allocated task. However, when process run for a longer time, wait for some other resource or get stuck in deadlock, they continuously consume memory. At such times, we may want to release the memory and other resources by killing such processes.
To kill a particular process, we need to determine its PID, ie. Process ID. This can be done using the 'ps' command. 'ps' command takes the snapshot of the memory when it runs and lists all the running process statistics. This is the very reason for including itself in the list.
$ ps

The next step is to use the 'kill' command along with the PID of the process you wish to kill. You can specify the 'kill' command to be non-maskable. Using -9 with the 'kill' command makes it non-maskable.
$ kill -9

Suppose you have a long running process - 'grep' in Unix which you desire to purge or kill. You first need to find out the PID of this process.

dev@pb456-5878:/app/data/vishal> ps
  PID TTY          TIME CMD
 1983 pts/44   00:00:00 more
 2635 pts/44   00:00:00 vi
 6521 pts/44   00:00:00 more
 9622 pts/44   00:00:00 ps
12458 pts/44   00:00:00 more
12605 pts/44   00:00:00 man
12618 pts/44   00:00:00 sh
12627 pts/44   00:00:00 less
13084 pts/44   00:00:00 cat
13161 pts/44   00:00:00 more
15732 pts/44   00:00:00 more
15933 pts/44   00:00:00 more
16603 pts/44   00:00:00 more
17444 pts/44   00:00:00 grep
17930 pts/44   00:00:00 more
19905 pts/44   00:00:00 more
20264 pts/44   00:00:00 more
21976 pts/44   00:00:00 more
27540 pts/44   00:00:01 bash
31061 pts/44   00:00:00 ksh
32137 pts/44   00:00:00 more


Now use the below command to kill the 'grep' process.


dev@pb456-5878:/app/data/vishal> kill -9 17444
[7]   Killed                  grep garantia  (wd: dev@pb456-5878:/app/data/vishal)
(wd now: dev@pb456-5878:/app/data/vishal)
You can verfiy the the successful purge of the 'grep' process by again issuing the 'ps' command.
dev@pb456-5878:/app/data/vishal> ps
  PID TTY          TIME CMD
 1983 pts/44   00:00:00 more
 2635 pts/44   00:00:00 vi
 6521 pts/44   00:00:00 more
 9622 pts/44   00:00:00 ps
12458 pts/44   00:00:00 more
12605 pts/44   00:00:00 man
12618 pts/44   00:00:00 sh
12627 pts/44   00:00:00 less
13084 pts/44   00:00:00 cat
13161 pts/44   00:00:00 more
15732 pts/44   00:00:00 more
15933 pts/44   00:00:00 more
16603 pts/44   00:00:00 more
17930 pts/44   00:00:00 more
19905 pts/44   00:00:00 more
20264 pts/44   00:00:00 more
21976 pts/44   00:00:00 more
27540 pts/44   00:00:01 bash
31061 pts/44   00:00:00 ksh
32137 pts/44   00:00:00 more
You can see in the above list that 'grep' is not present.