How to kill processes in Linux using kill and killall: a complete guide

- Andrés Cruz - ES En español

How to kill processes in Linux using kill and killall: a complete guide

If you use Linux, it has surely happened to you: the computer slows down, Eclipse freezes, Android Studio stops responding, or Chrome starts behaving strangely. In most cases, this happens because a process consumes too many resources or gets stuck in a state from which it cannot exit on its own. The fastest and most effective solution is to terminate that process directly from the terminal to free up memory and CPU, without needing to reboot the system.

Depending on the Linux distribution you use, its version, installed programs, and available resources, many things can go wrong and cause the computer—or a particular program—to "hang". This happens quite frequently in resource-heavy applications, such as Android Studio, emulators, local AIs, XCode, or video games.

In these cases, the right thing to do is to "kill" or terminate the process (or processes) associated with the program to free up system resources and, if necessary, restart the application from scratch.

How to kill processes in Linux: basic concepts

In Linux, a process is the execution of a program in memory. The operating system assigns resources to each process (CPU, RAM, file descriptors) and manages them independently. To control and terminate them when necessary, there are command-line tools like kill and killall. Both allow finishing processes, but they work differently and apply to different situations.

The kill and killall commands to terminate processes

There is a pair of commands that allow performing this task in a simple and direct way from the terminal: kill and killall. Both send signals to the process to indicate that it must stop, although they differ in how they identify the target process.

What a PID is and why it matters

Every process in Linux has a PID (Process ID), a unique number that identifies it while it is running. The kill command needs this PID to know exactly which process to terminate. This makes it the ideal option when you want absolute precision: for example, closing a specific instance of Chrome that hung, without affecting other windows or tabs that are still working fine.

How to get the PID with ps and grep

To locate the PID of a process, you can use the combination of ps aux with grep:

ps aux | grep process_name

For example, if you want to find Gedit's PID:

ps aux | grep gedit

This returns something like:

andres   10081  0.3  0.7 666388 35004 ?        Sl   11:21   0:00 /usr/bin/gedit

The PID here is 10081. The second column of the ps aux output is always the process identifier.

Practical example of the kill command

To use the kill command you need to know the identifier of the process you want to terminate. For that, first run ps aux to list all active system processes:

...
3036 ?        S      0:00 /opt/google/chrome/chrome --type=zygote --enable-crash-reporter=DFD73122-B4B8-416D-A254-9ABF21A8B36E
 3056 ?        Sl     4:35 /proc/self/exe --type=gpu-process --channel=3019.0.65190361 --enable-crash-reporter=DFD73122-B4B8-416D-A254-9ABF2
 3062 ?        S      0:00 /proc/self/exe --type=gpu-process --channel=3019.0.65190361 --enable-crash-reporter=DFD73122-B4B8-416D-A254-9ABF2
 3145 ?        Sl     0:08 /opt/google/chrome/chrome --type=renderer --lang=es-419 --force-fieldtrials=AffiliationBasedMatching/Enabled/Audi
 3149 ?        Sl     0:00 /opt/google/chrome/chrome --type=renderer --lang=es-419 --force-fieldtrials=AffiliationBasedMatching/Enabled/Audi
 3152 ?        Sl     0:00 /opt/google/chrome/chrome --type=renderer --lang=es-419 --force-fieldtrials=AffiliationBasedMatching/Enabled/Audi
 3225 ?        Sl     0:00 /usr/libexec/gvfsd-trash --spawner :1.5 /org/gtk/gvfs/exec_spaw/0
 3268 ?        Sl     1:01 /opt/google/chrome/chrome --type=renderer --lang=es-419 --force-fieldtrials=AffiliationBasedMatching/Enabled/Audi
 3525 ?        S      0:00 /home/andres/sts-bundle/sts-3.6.4.RELEASE/STS
 3527 ?        Sl     5:07 /usr/bin/java -Dosgi.requiredJavaVersion=1.6 -Xms40m -Xmx768m -XX:MaxPermSize=256m -jar /home/andres/sts-bundle/s
 4306 ?        Sl     6:14 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.60-14.b27.fc21.x86_64/bin/java -Dcatalina.base=/home/andres/sts-bundle/pivo
 4349 ?        Ss     0:02 postgres: postgres oo 127.0.0.1(50352) idle
 4468 ?        Sl     0:17 /opt/google/chrome/chrome --type=renderer --lang=es-419 --force-fieldtrials=*AffiliationBasedMatching/Enabled/Aud
 5568 ?        Sl     0:16 /opt/google/chrome/chrome --type=renderer --lang=es-419 --force-fieldtrials=*AffiliationBasedMatching/Enabled/Aud
 6278 ?        Sl     0:02 /usr/bin/nautilus --gapplication-service
 7712 ?        Sl     0:03 /usr/bin/pgadmin3
 8381 ?        S      0:00 [kworker/u4:2]
 9967 ?        Rl     0:00 /usr/libexec/gnome-terminal-server
 9971 pts/0    Ss     0:00 bash
10081 ?        Sl     0:00 /usr/bin/gedit --gapplication-service
10088 ?        S      0:00 [kworker/u4:1]
...

Filtering processes with ps and grep

As you can see, the system manages dozens of processes in parallel, most of them internal to the operating system itself and not initiated directly by you. To quickly find the process you are interested in, combine ps with grep:

ps aux | grep gedit

This returns a much smaller and more readable list:

andres   10081  0.3  0.7 666388 35004 ?        Sl   11:21   0:00 /usr/bin/gedit --gapplication-service
andres   10231  0.0  0.0 113020  2248 pts/0    S+   11:24   0:00 grep --color=auto gedit

Now we can terminate the Gedit program associated with PID 10081 with the following command:

kill 10081

If the process no longer exists, Linux will let you know with a message like this:

kill 41076
bash: kill: (41076) - No such process

The basic syntax of the command is:

kill PID

By default, kill sends the SIGTERM (15) signal, which asks the process to close cleanly—saving files, closing connections, freeing resources—. If the process does not respond to that signal, you can force its immediate termination with SIGKILL (9):

kill -9 PID

Most common kill signals and when to use them:

  • SIGTERM (15): terminates the process gracefully. It is the default signal and the one you should always use as a first attempt.
  • SIGKILL (9): forces immediate termination. The process cannot ignore it or clean up resources. Use it only if SIGTERM did not work.
  • SIGHUP (1): reloads the process configuration without stopping it. Very useful in servers and daemons like nginx or sshd.
  • SIGINT (2): interrupts foreground processes, equivalent to pressing Ctrl + C in the terminal.
  • General rule: always start with SIGTERM and resort to SIGKILL only if the process does not respond after several seconds.

The killall command: killing processes by name

With killall things get much simpler: you don't need to look up the PID, you just need to know the name of the process you want to terminate. It is the fastest option when you know exactly which program you want to close and you don't mind closing all its instances at once.

Key differences between kill and killall:

  • kill: needs the PID → precise control over a specific process.
  • killall: uses the process name → closes all active instances with that name.

For example, to terminate all instances of Firefox:

killall firefox

To close Gedit:

killall gedit

To close pgAdmin3:

killall pgadmin3

To close Google Chrome:

killall google-chrome

To close Nautilus (the GNOME file manager):

killall nautilus

If the process you are trying to terminate is not running, killall will let you know:

killall pgadmin3
pgadmin3: no process found

As you can see, the only condition to use killall is to know the exact name of the process as it appears in the system. And here comes the most important detail: killall is case-sensitive, so Firefox and firefox are different processes for this command.

kill vs killall: which one to use in each situation

  • Use kill when you want to close a specific process and you already know its PID. It is the safest and most controlled option.
  • Use killall when you want to close all instances of a program at once and you know its exact name.
  • In servers or critical systems, always verify which processes you are going to terminate before running either command. Killing the wrong process with root privileges can destabilize the system.

Common errors when using kill and killall

  • killall is case-sensitive: firefox is not the same as Firefox. If the name doesn't match exactly, the command won't find the process.
  • Closing unintended instances: killall terminates all instances with that name. If you have multiple Firefox windows open, it will close them all without distinction.
  • Using -9 unnecessarily: kill -9 or killall -9 force immediate closure without giving the process time to save its state. This can lead to data loss or corrupted files. Use it only as a last resort.
  • Killing system processes as root without precaution: terminating essential operating system processes can cause a complete Linux crash or leave it in an unstable state.
  • Not checking child processes: when you terminate a parent process, Linux also closes its child processes. This can cause unexpected work loss if you don't properly identify the process hierarchy first.

Practical tips for terminating processes without breaking the system

  • Always identify the PID or exact process name before running any command.
  • Avoid kill -9 unless the process has been unresponsive to SIGTERM for a while.
  • To identify which processes consume the most resources, use top or its improved version htop (more visual and interactive). Both show the CPU and memory consumption of each process in real time.
  • Combine ps aux | grep name to filter specific processes and confirm their PID before running kill.
  • If you need to get just the PID quickly without parsing the ps output, you can use pgrep process_name or pidof process_name. Both return only the matching PIDs.

Frequently asked questions about kill and killall in Linux

  • What is the difference between kill and killall?
    • kill acts on a process using its PID, giving you precise control. killall acts by name and closes all instances of that process at once.
  • What happens if I use kill -9?
    • It forces the immediate closure of the process without allowing it to clean up its state or save data. It can cause loss of files or unsaved data. Use it only if the process does not respond to SIGTERM.
  • How to find the PID of a process in Linux?
    • You can use ps aux | grep process_name, pidof process_name, or pgrep process_name. All three return the PID of the searched process.
  • Is it dangerous to use killall?
    • It can be if you are not careful: it closes all instances of the process without distinguishing which is which. On servers, a killall java can take down multiple services at the same time. Use it wisely.
  • What command to use if Linux freezes?
    • If the terminal still responds, run killall process_name or kill -9 PID to force close the problematic program. If the graphical interface does not respond, xkill lets you click on the frozen window to terminate the associated process directly.
  • How to kill the Discord process in Linux?
    • You can use killall Discord (with a capital D, as it appears in the system) or locate its PID with pgrep -l discord and then run kill PID.

Conclusion

Mastering kill and killall gives you real control over your Linux system. You can resolve freezes, free up resources, and keep working without needing to reboot. The key is choosing the right command for each situation: kill when you need surgical precision over a specific process, and killall when you want to close all instances of a program at once. Combined with ps aux, pgrep, top, and htop, you have all the tools you need in the terminal to manage processes safely and efficiently. And remember: always start with SIGTERM and use SIGKILL only when there is no other choice.

Learn how to kill processes in Linux using kill and killall: how to obtain the PID, use SIGTERM and SIGKILL, use killall by name, and close applications like Discord, Chrome, and more.


Únete a la comunidad de desarrolladores que han decidido dejar de picar código y empezar a construir productos reales. Recibe mis mejores trucos de arquitectura cada semana:

I agree to receive announcements of interest about this Blog.