Tuesday, May 20, 2014

How to kill a linux process

What is a PID?

A Linux or Unix process is running instance of a program. For example, Firefox is a running process if you are browsing the Internet. Each time you start Firefox browser, the system is automatically assigned a unique process identification number (PID). A PID is automatically assigned to each process when it is created on the system. To find out PID of firefox or httpd process use the following command:
  pidof httpd  
  pidof apache2   
  pidof firefox

kill command syntax

The syntax is:

kill [signal] PID
kill -15 PID
kill -9 PID
kill -SIGTERM PID
kill [options] -SIGTERM PID


What Linux or Unix permissions do I need to kill a process?

Rules are simple:
  1. You can kill all your own process.
  2. Only root user can kill system level process.
  3. Only root user can kill process started by other users.

kill command examples

In this example, I am going to kill lighttpd server.

Step #1: Find out the PID (process id)

Use the ps or pidof command to find out PID for any program. For example, if process name is lighttpd, you can use any one of the following command to obtain process ID:
 
pidof lighttpd
 
Sample outputs:
3486
OR
 
ps aux | grep lighttpd
 
 

How can I kill two or more PIDs?

The syntax is as follows to kill two or more PIDs as required can be used in a single command:
  kill pid1 pid2 pid3 kill -15 pid1 pid2 pid3 kill -9 pid1 pid2 pid3 kill -9 3546 5557 4242

Say hello to killall command

This is a Linux only command. to kill processes by name. So no need to find the PIDs using the 'pidof process' or 'ps aux | grep process' command. Do not use killall command on Unix operating systems. This is a Linux specific command.

The syntax is

 killall Process-Name-Here

To kill the lighttpd server, enter
:
# killall -15 lighttpd
OR
# killall -9 lighttpd
To kill the Firefox web-browser process, enter:

# killall -9 firefox-bin


As I said earlier, the killall command on UNIX-like system does something else. It kills all process and not just specific process. Do not use killall on UNIX system.
 


No comments: