CodeGym /Courses /Docker SELF /Terminating processes: commands `kill`, `killall`, `pkill...

Terminating processes: commands `kill`, `killall`, `pkill`

Docker SELF
Level 2 , Lesson 5
Available

Terminating processes: commands kill, killall, pkill

1. Terminating Processes

Today, we’re gonna take things up a notch and learn some commands that’ll help you efficiently terminate processes. Get ready, because your days of putting up with "frozen" programs are almost over!

Every running app or command in Linux is basically a process. But what happens if a program freezes, eats up 100% of your CPU resources, or just stops functioning properly? That’s where you, as an admin (or dev), step in and terminate the "rogue" process. Linux has a whole arsenal of tools for this: kill, killall, and pkill. Don’t worry, despite the intimidating names, we’re not here to break everything. Think of it more like an elegant way to keep things in check.


2. Command kill

Basics

The kill command terminates a process by its Process ID (PID). PID is a unique number the system assigns to each process. For example, if you want to terminate a process with PID 1234, use the following command:

kill 1234

Linux is quite flexible by nature, so the kill command doesn't send a "die now" order but rather a termination request. This request is called a "signal". By default, kill sends the SIGTERM (15) signal.

What’s a signal?

Linux uses signals to communicate with processes. It's like sending postcards with different intentions:

  • SIGTERM (15): "Please, wrap it up". This is a polite request for the process to finish execution.
  • SIGKILL (9): "Alright, that's it, you're fired!" Forcefully terminates the process.
  • SIGHUP (1): "Hey, reload the configuration." Used to restart some processes without stopping them.

Example of forcefully terminating a process:

kill -9 1234

If a process refuses to terminate with SIGTERM, you can send a tougher signal — SIGKILL.

How to find the PID of a process?

To use the kill command, you need to know the PID of the process. Helpful commands like ps, top, or htop can assist you here. For instance, the output of the ps aux command will show you all running processes:

ps aux | grep firefox

This query will find the Firefox process and display its PID.

The process is terminated, you can relax. But what if there are multiple "stuck" processes? This is where the killall and pkill commands come into play.


3. The killall Command

Convenience for Mass Termination

The killall command allows you to terminate all processes with a certain name. No need to manually look for the PID, everything is way simpler. For example, if you want to close all running instances of Firefox:

killall firefox

Just like the kill command, killall sends signals to processes. You can specify a specific signal:

killall -9 firefox

Helpful Tip

Be careful with commands like killall. If you accidentally specify a process that's too generic, like killall python, you might end up terminating all running Python apps, including server tasks or important scripts.

What Signals Are Supported?

For all commands kill, killall, and pkill, you can check the list of available signals using the following command:

kill -l

4. Command pkill

Finding processes by name (or part of it)

The pkill command works similarly to killall but is more flexible. It allows you to terminate processes that match a certain pattern. For example:

pkill fire

This command will terminate all processes that contain the word "fire" in their name. It's handy when the process name is long, or you only know part of it.

Terminating user processes

You can terminate processes that belong to specific users. For example:

pkill -u username

This will terminate all processes that belong to the user username.

Comparison of kill, killall, and pkill

Command Features
kill Terminates a process by PID. Works individually.
killall Terminates all processes with the specified name. Convenient for bulk termination.
pkill Terminates processes by name or pattern. Supports additional parameters like username.

5. Practice: Terminating Processes

Let's try everything we've learned in action. First, let's start a few long-running processes:

sleep 300 &
sleep 400 &
sleep 500 &

These commands will create three background processes with different wait times. Now you can work with the kill, killall, and pkill commands.

  1. View all running sleep processes using the ps command:
ps aux | grep sleep

You will see the PID of each process.

  1. Terminate a process with a PID, for example, 1234:
kill 1234
  1. Terminate all sleep processes at once:
killall sleep
  1. Start a few more sleep processes and terminate them using pkill:
pkill sl

6. Common Mistakes and Important Nuances

If you're trying to terminate a process that you don't have permissions for (like another user's process), the command will throw an error. You need superuser privileges (sudo) for that. For example:

sudo kill 1234

Another source of confusion could be using overly aggressive signals like -9. It's like pulling the plug on your computer: the system won’t have time to release resources or save data. Use SIGKILL only as a last resort.

Be careful with killall and pkill, especially when managing a server. Blindly killing processes can lead to loss of important data.


Why is it important to know process termination commands?

These commands are super useful, from killing frozen apps to managing server processes. In job interviews, knowing kill and pkill can highlight your skills, especially if you're into system admin work or backend tasks.

You can use this knowledge to create scripts that automatically kill rogue processes or free up ports. For instance, before starting a server that occupies port 8080, you could use:

pkill -f "port 8080"

The flexibility and power of these commands make them a must-have tool for any Linux specialist.

1
Task
Docker SELF, level 2, lesson 5
Locked
Terminating a Process by PID
Terminating a Process by PID
1
Task
Docker SELF, level 2, lesson 5
Locked
Terminating all processes with the same name
Terminating all processes with the same name
1
Task
Docker SELF, level 2, lesson 5
Locked
Terminating processes by pattern
Terminating processes by pattern
1
Task
Docker SELF, level 2, lesson 5
Locked
Terminating processes of a specific user
Terminating processes of a specific user
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION