Oct 15, 2023 5 min read

Kill Command in Linux

Discuss the kill command in linux with our step-by-step tutorial. It is used to terminate or send signals to running processes.

Kill Command in Linux
Table of Contents

Introduction

Before we start discussing about kill command in linux, let's first understand-What is a Kill Command ?

The kill command in Linux is used to terminate or send signals to running processes. It allows users to forcefully stop processes by specifying their process IDs (PIDs) or by sending specific signals to control the behavior of a process.

Linux is a fantastic and sophisticated operating system, but it is not perfect. Some applications may behave abnormally from time to time, becoming unresponsive or demanding a large number of system resources. Because the original application process never entirely closes down, unresponsive programs cannot be restarted. Either restart the system or kill the program process are the only options.

There are a number of utilities available to help you terminate errant processes, with kill being the most popular.

In this tutorial, we will discuss briefly about the kill command.

Kill Command

Most Bourne-derived shells, such as Bash and Zsh, include a kill shell. The shells and the standalone /bin/kill executable have slightly different command behavior.

To see all locations on your system that include kill, use the type command.

type -a kill
Output

kill is a shell builtin
kill is /bin/kill

When you type kill, the result above demonstrates that the shell builtin takes precedence over the standalone executable. Type the entire path to the file /bin/kill if you want to utilize the binary. We'll use the Bash builtin in this tutorial.

The kill command is written in the following format:

kill [OPTIONS] [PID]...

The kill command transmits a signal to selected processes or process groups, instructing them to follow the signal's instructions. If you don't specify a signal, it defaults to -15 (-TERM).

The following are the most regularly used signals:

  1. 1 (HUP) - Reload a process.
  2. 9 (KILL) Kill a process.
  3. 15 (TERM) - End a process gracefully.

Invoke the command with the -l option to acquire a list of all possible signals:

kill -l

Signals can be defined in one of the three ways:

  • Making use of numbers (e.g., -1 or -s 1).
  • Using the prefix "SIG" (e.g., -SIGHUP or -s SIGHUP).
  • Without the prefix "SIG" (e.g., -HUP or -s HUP).

The commands listed below are interchangeable:

kill -1 PID_NUMBER
kill -SIGHUP PID_NUMBER
kill -HUP PID_NUMBER

One of the following PIDs can be sent to the kill command:

  • The signal is sent to the process with ID equal to the PID if PID is greater than zero.
  • The signal is given to all processes in the current process group if PID is equal to zero. To put it another way, the signal is broadcast to all processes with the same GID as the shell that issued the kill command. To see the process group IDs, use the ps -efj command (GIDs).
  • If PID is set to -1, the signal will be transmitted to all processes that have the same UID as the user who issued the command. The signal is issued to all processes except init and the kill process itself if the invoking user is root.
  • If the PID is less than -1, the signal is issued to all processes in the process group eq, with GID equal to the PID's absolute value.

Regular users can only send signals to their own processes, but not those that belong to other users. The root user, on the other hand, has the ability to transmit signals to other users' processes.

Terminating Processes Using the kill Command

To use the kill command to terminate or kill a process, you must first locate the process ID number (PID). You may do this with top, ps, pidof, and pgrep, among other commands.

Let's imagine your Firefox browser has become uncooperative and you need to terminate it. Use the pidof command to find the browser PIDs:

pidof firefox

The IDs of all Firefox processes will be printed by this command:

Output

6263 6199 6142 6076

You can kill all of the processes by sending the TERM signal once you know their numbers:

kill -9 6263 6199 6142 6076

You can combine the above commands into one instead of looking for PIDs and then killing the processes:

kill -9 $(pidof firefox)

Reloading Processes Using the kill Command

Sending the HUP signal, which instructs processes to refresh their settings, is another popular use case for kill.

To reload Nginx, for example, you must send a signal to the master process. The nginx.pid file, which is normally placed in the /var/run directory, contains the process ID of the Nginx master process.

To find the master PID, use the cat command:

cat /var/run/nginx.pid
Output

30251

Reload the Nginx settings once you've discovered the master PID by typing:

sudo kill -1 30251

Run the command above as root or as a user with sudo capabilities.

FAQs of Kill Command in Linux

How do I use the kill command to terminate a process?

To terminate a process using kill, you need to specify its PID. For example, kill 1234 will attempt to terminate the process with PID 1234.

Can the kill command terminate multiple processes simultaneously?

Yes, you can provide multiple PIDs to the kill command, separated by spaces. For example, kill 1234 5678 will attempt to terminate both processes with PIDs 1234 and 5678.

What are signals in the context of the kill command?

Signals are messages that can be sent to running processes to control their behavior. The kill command can be used to send signals to processes, such as terminating them (SIGTERM) or forcing them to reload their configuration (SIGHUP).

How can I send a specific signal to a process using the kill command?

By default, the kill command sends the SIGTERM signal (15) to a process. To specify a different signal, you can use the -s option followed by the signal name or number. For example, kill -s SIGUSR1 1234 sends the SIGUSR1 signal to the process with PID 1234.

What happens if I send the SIGKILL signal to a process using kill?

The SIGKILL signal (9) is a special signal that immediately terminates a process without allowing it to clean up or perform any final actions. It forcefully stops the process, and its resources are released.

Can I kill a process owned by a specific user using the kill command?

Yes, if you have the appropriate permissions, you can use the kill command with the -u option followed by the username to terminate processes owned by a specific user. For example, kill -u username will attempt to kill all processes owned by "username".

What is the difference between kill and pkill?

While the kill command terminates processes based on their PIDs, the pkill command allows you to terminate processes based on their names or other attributes. pkill offers more flexibility in selectively killing processes based on certain criteria.

Conclusion

To deliver a signal to processes, use the kill command. SIGKILL or -9 is the most commonly used signal, which terminates the specified processes.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.