How to kill a process in Linux

There are many program which may either be running by a user or by the Operating System itself. These programs which are running on the system are called processes. A process gets terminated on its own when they’re done with their tasks or when you ask them to quit by clicking on the Close button. However, sometimes a process can hang up or consume a lot of CPU or RAM. In these situations, you would want to manually kill the process.

How can we locate these processes?

You can use below commands to locate these processes depending upon different situations:

  1. top
  2. ps
  3. pidof
  4. pgrep

You can kill a process by its name or PID (Process ID). The PID is a number that uniquely identifies a process. Killing by the PID is useful when you want to kill only a specific process and killing by the process name is useful when you want to kill all running instances of a particular program.

Locate the processes with top command

When you run top command, it will give you a list of processes along with their PIDs. You can check PID of the process you want to kill.

Locate the processes with ps and grep commands

# ps aux

Where,

  • a – will show processes for all users.
  • u – will display the user who is running those process.
  • x – will show all processes. It will include the processes running in a GUI environment.

It will also show the list of processes including the process names and their PIDs. You can use grep command to filter the list for any specific process you are looking for.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

For example,

# ps aux | grep -i mysql

Locate the PID with pidof and pgrep

If you know the process name, you can simply find its PID by running pidof command. For example, you know there is a process named mysqld and you want to find its PID, you can simply run below command:

# pidof mysqld

If you don’t know the full name the process, you can use pgrep instead of pidof. Example below will give you the results which matches mysql.

# pgrep mysql

Kill a Process

After locating the processes using any of the above methods, you can proceed further with killing a process. But how to determine what processes you can kill.

If you are a normal user, you can kill your own processes but not the ones that belong to the other users. Both top and ps will show the user under which a process is running. But a root user can kill all the process.

Syntax of kill command:

# kill [signal or option] PID(s)

There are various signals for a kill command.

Signal Name      Signal Value       Behaviour
SIGHUP			              1				          Hangup
SIGKILL			              9				          Kill Signal
SIGTERM			           15			            Terminate

SIGTERM – It is the default and safest way to kill a process.

SIGHUP – It is less secure way of killing a process as SIGTERM.

SIGKILL – It is the most unsafe way among the above three, to kill a process which terminates a process without saving.

The Linux kernel maintains some information related to the state of a process. When a process terminates, the kernel must keep the information so the parent process can find out if the child process was able to complete its task and whether it got terminated on its own or it got killed. Until the parent has done that, we’ll see these zombie processes in the list of processes. You can’t kill a zombie process because it’s just an entry in the list of all processes and it doesn’t have an actual process associated with it. When a process performs input/output operations, it is in a state of uninterruptible sleep. You can’t kill that process as well while its in uninterruptible sleep. You can see if a process is in the zombie (Z) or uninterruptible sleep (D) state by looking at the 8th column of the top / ps output.

Commands to Kill a process

  • killall
  • pkill
  • kill
  • top

Kill a process with killall command

Kill a process named mysqld, run:

# killall mysqld

To forcibly kill the process with SIGKILL, run:

# killall -9 mysqld

You can also use -SIGKILL instead of -9.

You can also kill a process that has been running for a certain period of time with the -o and -y flags.

Example below shows, to kill a process which is running for more than 30 minutes.

killall -o 30m <process-name>

To kill a process which is running for less than 30 minutes. Example:

killall -y 30m <process-name>

Similarly, use the following abbreviations for the respective units of time:

s seconds
m minutes
h hours
d days
w weeks
M months
y years

 

Kill a process with pkill command

When you are not sure about the exact process name. Just like pgrep, pkill allows you to kill processes based on partial matches. For example, if you want to kill all processes containing the name mysql in the name.

# pkill mysql
# pkill -9 mysql
Or
# pkill -SIGKILL mysql

Kill a process with kill command

You can terminate a process using the kill command after you have found the PID of that process. Below is an example where mysqld process has the PID 3329.

# kill 3329

As mentioned above, the default is to use a SIGTERM. To use a SIGKILL, use -9 or -SIGKILL.

# kill -9 3329

Kill a process with top command

We can very easily kill processes using the top command. Just search for the process you want to kill and keep a note of the PID. Then press k on your keyboard while top command is running. It will prompt you to enter the PID of the process that you want to kill. Now enter the PID and hit enter. Now it will ask you to enter the signal you want to use to kill the process. If you want to use SIGTERM(15), then do not type anything and simply press enter as it is the default signal. If you want to use SIGKILL(9), then type 9 and press enter.

Note: If you leave the PID blank and hit enter directly, it will terminate the topmost process in the list. You can scroll down with the arrow keys on your keyboard and change the process you want to kill as well.

   That’s it in this article, hope you enjoyed it. Please share it across if you think it’s good.

 

2 thoughts on “How to kill a process in Linux

  • May 9, 2020 at 2:52 am
    Permalink

    There certainly are many ways to find (ps, grep, and awk) processes and to send a kill signals to them.

    The dimension that you are omitting here is the idea that most signals can be trapped and ignored. You mention the idea that SIGKILL (9) is “unsafe,” but the real issue here is that it cannot be trapped by a process. SIGKILL will end a process without allowing it to clean up any temporary files, close sockets, or whatever (etc …).

    With that being said, it is technically inaccurate to say that a process will end when it is done with its task. Daemon processes may sit idle until they have work to do. They do not quit when their work is done. Instead, they go back to waiting for more work to do.

    For example, the main Apache httpd process may spawn, oh, say five or more sub / worker processes. These process are not killed after responding to an HTTP request. They wait for the next incoming HTTP request.

    I like to look at processes in Linux / UNIX as one of the fundamental entities of the system. Hence, I have been writing a “Process” library in bash. Check it out. You might argue with my style (or misunderstand it), but you might learn something, too.

    https://github.com/delturge/Bash-Library/blob/master/Entities/Process.sh

    Lastly, the process group ID (PGID) is something worth investigating. Those were the next set of functions I was going to add to my “Process” library. Being able to kill multiple processes with one kill command is something worth looking into (but it’s not as straight forward as first meets the eye).

    Peace.

    Anthony

    Bonus
    ======

    Determine if a process exists.

    function isProcess ()
    {
    kill -s EXIT $1 > /dev/null 2>&1
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *