HiBlog Linux and Windows Server

How to Kill a Process in Linux with kill and kill -9

  • Ali GhanimatiAuthor
  • Last updated
  • 8 minutesReading time
  • 1,552Views
  • Ali Ghanimati
  • Last updated:
  • 8 min read
  • 1,552 views
How to Kill a Process in Linux with kill and kill -9

When you work on a Linux server, a program may freeze and stop answering any command, a script may get stuck in a loop and take all the CPU, or a process may fill the server’s RAM until the site slows down. Many programs have their own proper way to close, but when a program hangs, the only way out is to stop it yourself; this is what Linux calls killing a process.

In this tutorial you first learn to find the right process with ps, pgrep, pidof and top, and then stop it with the kill, pkill and killall commands. We also explain the difference between the SIGTERM and SIGKILL signals, the famous kill -9, and why services should be stopped with systemctl.

Short answer: find the process number (PID) with pgrep -l name or ps aux, and first ask it to close cleanly with kill PID. If it is still running a few seconds later, kill -9 PID stops it at once. For a process of another user or root, run the command with sudo.

Finding the process number (PID) or name

Every process in Linux has a unique number called the PID (Process ID). Before stopping a process, you need its PID or exact name. Four main tools do this.

The ps command

ps shows the list of running processes. With this combination you see every process of every user, with all its details:

ps aux
The list of Linux processes with ps aux
  • a: shows the processes of all users, not only yours.
  • u: adds the user, CPU and memory percentages and start time of each process.
  • x: also shows processes not attached to a terminal, such as services.

The PID column is the process number and the COMMAND column the program name. To find a particular program, filter the output: ps aux | grep nginx. As the picture shows, ps -aux with a dash works too, but the correct, standard form has no dash.

The pgrep and pidof commands

pgrep searches process names with a pattern and returns their PIDs. The pattern is a regular expression; for example ^php means names that start with php. Its most useful options:

  • -l: shows the process name next to the PID.
  • -u user: only the processes of one user.
  • -x: only names that match the pattern exactly.
  • -n and -o: only the newest or the oldest process.
  • -f: searches the pattern in the process’s whole command line, not only the program name; for example to find a Python script called worker.py.

For example, pgrep -l -u root shows the name and PID of all processes of the root user:

Output of pgrep -l -u root in Linux

pidof is simpler: it takes a program’s exact name and returns the PIDs of all its processes. For example pidof sshd:

Finding the PID of the sshd service with pidof

The -s option returns a single PID, and -o PID leaves the given number out of the result.

The top command

top is a live list of processes that keeps refreshing and shows how much CPU and memory each one uses. When the server is slow and you do not know who is to blame, start here:

top
Processes and their CPU and memory use in top

Press P to sort the list by CPU use and M to sort it by memory use. Press q to leave top.

SIGTERM or SIGKILL? Close it properly first

Killing a process really means sending it a signal. These are the two main signals for stopping one:

  • SIGTERM (number 15): the default signal. It asks the process to close and gives it time to finish its work, save files and remove its temporary files. A program can ignore this signal.
  • SIGKILL (number 9): stops the process at once, no questions asked, and the program cannot block it. But it also gives no chance to save and tidy up; half-written data or a locked file may be left behind.

So the rule is simple: always SIGTERM first, and SIGKILL only if the process has not closed after a few seconds. That way the program closes cleanly and no data is lost. kill -l lists all the signals.

Stopping a process with kill and kill -9

The kill command sends a signal to a PID. If you do not give a signal number, it sends SIGTERM:

kill 1234
kill -9 1234

The first line asks process 1234 to close, and the second stops it at once with SIGKILL. kill -9 is the same as kill -KILL or kill -SIGKILL. You can also write several PIDs in a row: kill 1234 5678.

Each user can stop only their own processes. For a process of another user or root, run the command with sudo: sudo kill -9 1234. If you get Operation not permitted without sudo, this is why.

Stopping by name: pkill and killall

When you do not want to look up the PID first, these two commands stop a process by its name, and they send SIGTERM by default.

pkill

pkill has the same options as pgrep, but instead of showing processes, it stops them. Before pkill, try the same pattern with pgrep to see exactly which processes it picks:

pgrep -l php-fpm
sudo pkill php-fpm
sudo pkill -9 -f worker.py
pkill -n -u ali

The second line closes all processes whose name contains php-fpm with SIGTERM, the third stops the worker.py script with SIGKILL and the last one stops the newest process of the user ali. Note that pkill matches any name that contains the pattern; pkill php closes all php processes. For an exact name, add -x.

killall

killall stops all processes with exactly this name: sudo killall nginx. Its useful options:

  • -i: asks for confirmation before stopping each process.
  • -I: ignores upper and lower case in the name.
  • -u user: stops only the processes of one user.
  • -v: reports afterwards which processes got the signal.
  • -o and -y: only processes older or younger than the time you give; for example killall -o 15m php stops the php processes that have been running for more than 15 minutes.

If the system says killall: command not found, killall is in the psmisc package: sudo apt install psmisc.

Stopping a process from inside top

When you see the heavy process in top, you do not need to leave it: press k, type the PID and press Enter. top asks which signal to send; Enter sends SIGTERM, and if needed, type 9.

Stop services with systemctl, not kill

Programs such as nginx, Apache, MySQL and php-fpm are services that systemd manages. If you kill the process of such a service, systemd may start it again right away, as if you had done nothing. This is the right way:

sudo systemctl stop nginx
sudo systemctl restart nginx
systemctl status nginx

The status output also shows the service’s main process number as Main PID. If a service does not answer stop either, sudo systemctl kill --signal=SIGKILL nginx stops all of that service’s processes together.

Frequently asked questions

Why won’t a process close even with kill -9?

There are two common cases. A zombie process (Z in the STAT column of ps output) has in fact finished and is only waiting for its parent process to collect it; you need to stop the parent, whose number ps -o ppid= -p PID shows. A process in state D is waiting for the disk or network and answers no signal until that work is done.

How do I stop a program that holds a port?

If a program will not start with the message Address already in use, first see which process holds the port: sudo ss -tlnp | grep :80. The users column shows the program’s name and pid, and then you stop it with kill.

What does kill -9 $$ do?

The variable $$ holds the process number of the shell you are working in. So kill -9 $$ closes your current terminal and your SSH connection. Be careful not to run it on the wrong server!

A Linux server with enough resources

If you keep stopping processes that fill up the server’s RAM or CPU, it may be time to upgrade the server. A HiData VPS with full root access gives you a free hand to manage Linux. And on HiData’s Iran VPS, if the server becomes completely unreachable, you get in through the VNC console in the server panel without opening a ticket. To learn more about managing a Linux server, see our tutorial on file and folder permissions in Linux.

Technical sources

Share Post

hidata.org/en/blog/how-to-kill-a-process-in-linux/
In Social Media

Add Comment

Related Articles