WebMasterCampus
WEB DEVELOPER Resources

Linux kill Command

Learn Linux kill Command with examples


Linux kill Command

In Linux, we can use “kill” command send a signal to a process.

kill is a shell builtin in most Bourne-derived shells such as Bash and Zsh. The command behavior is slightly different between the shells and the standalone /bin/kill executable.

Use the type command to display all locations on your system containing kill:

>> type -a kill

kill is a shell builtin
kill is /usr/bin/kill
kill is /bin/kill

kill Command Syntax

>> kill [OPTIONS] [PID]...

The kill command sends a signal to specified processes or process groups, causing them to act according to the signal. When the signal is not specified, it defaults to -15 (-TERM).

The most commonly used signals are:

  • 1 (HUP) - Reload a process.
  • 9 (KILL) - Kill a process.
  • 15 (TERM) - Gracefully stop a process.

To get a list of all available signals, invoke the command with the -l option:

Signals can be specified in three different ways:

  • Using number (e.g., -1 or -s 1).
  • Using the “SIG” prefix (e.g., -SIGHUP or -s SIGHUP).
  • Without the “SIG” prefix (e.g., -HUP or -s HUP).

The following commands are equivalent to one another:

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

pidof command (to Find PIDs)

To find the browser PIDs use the pidof command.

>> pidof firefox

6263 6199 6142 6076

To terminate or kill a process with the kill command, first you need to find the process ID number (PID). You can do this using different commands such as top, ps , pidof and pgrep

kill Command to kill firefox process

>> kill -9 $(pidof firefox)

``

## kill -l

To get a list of all available signals, invoke the command with the -l option.

```bash
>> kill -l

kill Command Reloading Processes

Another common use case for kill is to send the HUP signal, which tells the processes to reload its settings.

For example, to reload Nginx , you need to send a signal to the master process. The process ID of the Nginx master process can be found in the nginx.pid file, which is typically is located in the /var/run directory

Use the cat command to find the master PID

>> cat /var/run/nginx.pid

30251

Once you found the master PID reload the Nginx settings by typing:

>> sudo kill -1 30251

The command above must be run as root or user with sudo privileges.

kill Command More Examples

Kill all processes you can kill.

>>  kill -9 -1

Translate number 11 into a signal name.

>>  kill -l 11

List the available signal choices in a nice table.

>> kill -L

Send the default signal, SIGTERM, to all those processes.

>> kill 123 543 2341 3453

kill Command Points to Remember

  • If PID is greater than zero, the signal is sent to the process with ID equal to the PID.
  • If PID is equal to zero, the signal is sent to all processes in the current process group. In other words, the signal is sent to all processes belonging to the GID of the shell that invoked the kill command. Use ps -efj command to view the process group IDs (GIDs).
  • If PID is equal to -1, the signal is sent to all processes with the same UID as the user invoking the command. If the invoking user is root, the signal is sent to all processes except init and the kill process itself.
  • If PID is less than -1, the signal is sent to all processes in the process group eq with GID equal to the absolute value of the PID.

Regular users can send signals to their own processes, but not those that belong to other users, while the root user can send signals to other user’s processes.

kill Command in Linux (Documentation)

>> man kill

NAME
       kill - send a signal to a process

SYNOPSIS
       kill [options] <pid> [...]

DESCRIPTION
       The default signal for kill is TERM.  Use -l or -L to list available signals.  Particularly useful signals in‐
       clude HUP, INT, KILL, STOP, CONT, and 0.  Alternate signals may be specified in three ways:  -9,  -SIGKILL  or
       -KILL.  Negative PID values may be used to choose whole process groups; see the PGID column in ps command out‐
       put.  A PID of -1 is special; it indicates all processes except the kill process itself and init.

OPTIONS
       <pid> [...]
              Send signal to every <pid> listed.

       -<signal>
       -s <signal>
       --signal <signal>
              Specify the signal to be sent.  The signal can be specified by using name or number.  The  behavior  of
              signals is explained in signal(7) manual page.

       -l, --list [signal]
              List signal names.  This option has optional argument, which will convert signal number to signal name,
              or other way round.

       -L, --table
            List signal names in a nice table.

       NOTES  Your shell (command line interpreter) may have a built-in kill command.  You may need to run  the  com‐
              mand described here as /bin/kill to solve the conflict.

EXAMPLES
       kill -9 -1
              Kill all processes you can kill.

       kill -l 11
              Translate number 11 into a signal name.

       kill -L
              List the available signal choices in a nice table.

       kill 123 543 2341 3453
              Send the default signal, SIGTERM, to all those processes.

SEE ALSO
       kill(2), killall(1), nice(1), pkill(1), renice(1), signal(7), skill(1)
Created with love and passion.