24/7 support, every day of the week 02192039 info@mizbandade.com
لینوکس

The kill Command in Linux

آموزش دستور Kill در لینوکس

Using the kill command in Linux

Linux is an excellent, advanced operating system, but it isn’t perfect. Sometimes a program misbehaves, stops responding or uses up a lot of system resources. An unresponsive program can’t simply be restarted, because its original process never fully shuts down. The only options are to restart the system or end the process, and the kill command lets you do the latter. In this guide we’ll introduce the kill command.

The kill command

kill is a shell builtin in most Bourne-derived shells such as Bash and Zsh. It behaves slightly differently in each shell and in the standalone /bin/kill executable. Use the type command to see every location on your system that contains kill:

type -a kill
kill is a shell builtin
kill is /bin/kill

The output shows that the shell builtin takes priority over the standalone executable and is used whenever you type kill. To use the binary, type the full path, /bin/kill. In this article we’ll use the Bash builtin. The syntax is:

kill [OPTIONS] [PID]...

kill sends a signal to the specified processes or process groups, which then act according to the signal. If no signal is given, it defaults to -15 (TERM). The most common signals are:

  • 1 (HUP): reload a process.
  • 9 (KILL): kill a process.
  • 15 (TERM): gracefully stop a process.

To list every available signal, run kill with the -l option:

kill -l
The list of signals from kill -l

Signals can be specified in three ways:

  1. By number (for example, -1 or -s 1).
  2. With the “SIG” prefix (for example, -SIGHUP or -s SIGHUP).
  3. Without the “SIG” prefix (for example, -HUP or -s HUP).

These commands are all equivalent:

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

The PIDs you pass to kill can be any of the following:

  • If the PID is greater than zero, the signal goes to the process with that ID.
  • If the PID is zero, the signal goes to every process in the current process group, that is, every process belonging to the GID of the shell that ran kill. Use ps -efj to see process group IDs (GIDs).
  • If the PID is -1, the signal goes to every process with the same UID as the user running the command. If that user is root, it goes to every process except kill itself.
  • If the PID is less than -1, the signal goes to every process in the process group whose GID equals the absolute value of the PID.

Regular users can send signals to their own processes but not to other users’ processes, while root can signal any process. To end a process with kill, you first need its process ID (PID), which you can find with commands such as top, ps, pidof and pgrep. Say Firefox has stopped responding and you need to kill it. Find its PIDs with pidof:

pidof firefox

This prints the IDs of every Firefox process:

6263 6199 6142 6076

Once you have the process numbers, you can kill them all at once with the KILL signal:

kill -9 6263 6199 6142 6076

Instead of looking up the PIDs first, you can combine both steps into one command:

kill -9 $(pidof firefox)

Reloading processes with kill

Another common use of kill is sending the HUP signal, which tells a process to reload its configuration. For example, to reload Nginx you send a signal to its master process. You’ll find the Nginx master process ID in nginx.pid, usually in /var/run. Read it with cat:

cat /var/run/nginx.pid
30251

Once you have the master PID, reload the Nginx configuration with:

sudo kill -1 30251

This command must be run as root or as a user with sudo privileges.

Summary

The kill command sends signals to processes, letting you stop, kill or reload them without restarting your system.

Leave a comment

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