Kill Command in Linux – Introduction
The ‘kill’ command in Linux is a command that is used to send signals to a process. The signals are used to ask a process to terminate gracefully, force a process to terminate immediately, pause or resume a process, or trigger custom actions in a process. The ‘kill’ command takes a process ID (PID) as its argument and sends a signal to the process with the specified PID. The default signal sent by the ‘kill’ command is ‘SIGTERM’, which asks the process to terminate gracefully. However, other signals can be specified with the -s option, such as ‘SIGKILL’, which forcefully terminates the process. The ‘kill’ command is a powerful tool for managing processes on a system of Linux.
You must first collect the PID of the process you wish to manage to use the ‘kill’ command. You can accomplish this in several methods, such as by using the ps command, the top command, or the pgrep command. The ‘kill’ command can be used to manage the process after you have its PID.
Basic steps for using the ‘kill’ command
STEP 01
Open a terminal window.
STEP 02
Use the appropriate command to obtain the PID of the process you want to manage. For instance, you can display a list of all processes that are now active on your system, together with their PIDs, by using the ‘ps’ command with the ‘aux’ option:
$ ps aux
Look for the process you want to manage in the output and note its PID.
STEP 03
To signal the process with the supplied PID, use the ‘kill’ command. The “kill” command by default emits the “SIGTERM” signal, instructing the process to die gracefully. For example, to terminate a process with a PID of 1234, you can use the following command:
$ kill 1234
If the process does not terminate after a certain period, you can end it abruptly by using the “SIGKILL” signal:
$ kill -9 1234
The ‘-9’ option specifies the ‘SIGKILL’ signal.
STEP 04
Check if the process has been terminated using the appropriate command. For instance, you can check to verify if the process is still active by using the ‘ps’ command once more:
$ ps aux | grep <process_name>
If the process is no longer running, you will not see any output.
These are the basic steps for using the ‘kill’ command to manage processes on a Linux system. However, some many other options and signals can be used with the ‘kill’ command, as demonstrated in the examples above. It is important to use the ‘kill’ command with caution, as it can cause unintended consequences if used improperly.
Using ‘Kill’ Command Examples
-
Terminate a process
$ kill PID
This command terminates a process with the specified process ID (PID). The default signal sent is SIGTERM, which asks the process to gracefully terminate.
-
Forcefully terminate a process
$ kill -9 PID
This command forcefully terminates a process with the specified PID by sending a SIGKILL signal, which immediately terminates the process.
-
Send a signal to a process
$ kill -SIGNAL PID
This command sends the specified signal to the process with the specified PID. Replace SIGNAL with the ‘signal’ you want to send, such as ‘HUP’, ‘INT’, or ‘USR1’.
-
Signal a process with a SIGINT signal.
$ kill -SIGINT PID
This command causes the process with the provided PID to receive a SIGINT signal, interrupting the process.
-
Send a SIGTERM signal to a process
$ kill -SIGTERM PID
This command asks the process with the provided PID to gracefully terminate by sending it a SIGTERM signal.
-
Send a SIGKILL signal to all processes with a given name
$ pkill -9 process_name
This command terminates all processes with the provided name by sending a SIGKILL signal to them.
-
Signal a process group using a SIGHUP signal.
$ kill -SIGHUP -PGID
Due to the fact that it sends a SIGHUP signal to all processes in the process group with the specified PGID, this command can be useful for reloading configuration files.
-
Give a process a SIGUSR1 signal.
$ kill -SIGUSR1 PID
This command sends the process with the specified PID a SIGUSR1 signal, which can be used to start a custom action.
-
Terminate all processes with a given name
$ pkill process_name
This command terminates all processes with the specified name.
-
Send a SIGINT signal to all processes with a given name
$ pkill -SIGINT process_name
This command causes all processes with the provided name to receive a SIGINT signal, interrupting them.
-
Send a SIGTERM signal to all processes with a given name
$ pkill -SIGTERM process_name
This command requests that all processes with the provided name gracefully terminate by sending them the SIGTERM signal.
-
Alert a process group using a signal
$ kill -SIGNAL -PGID
Using the supplied PGID, this command transmits the specified signal to every process in the process group.
-
Send a SIGUSR1 signal to all processes with a given name
$ pkill -SIGUSR1 process_name
All processes with the supplied name will receive a SIGUSR1 signal from this command, which can be used to initiate custom actions in the processes.
-
Terminate a process and all of its child processes
$ kill — -PGID
This command terminates the process group with the specified PGID, which includes the specified process and all of its child processes.
-
Send a SIGSTOP signal to a process:
This command suspends the process by sending a SIGSTOP signal to the PID that is supplied. The process can later be resumed using the kill -SIGCONT command.
$ kill -SIGSTOP PID
This can be useful for temporarily pausing a process to free up system resources or to debug it.
-
Send a SIGCONT signal to a process:
This command sends a SIGCONT signal to a process with the specified PID, which resumes a process that was previously stopped using the SIGSTOP signal.
$ kill -SIGCONT PID
-
Terminate all processes with a given name:
This command terminates all processes with the specified name.
$ pkill process_name
-
Send a SIGINT signal to all processes with a given name
This command causes all processes with the provided name to receive a SIGINT signal, interrupting them.
$ pkill -SIGINT process_name
-
Send a SIGTERM signal to all processes with a given name
This command sends a SIGTERM signal to all processes with the specified name, which asks them to gracefully terminate.
$ pkill -SIGTERM process_name
-
Send a SIGUSR1 signal to all processes with a given name:
All processes with the supplied name will receive a SIGUSR1 signal from this command, which can be used to initiate custom actions in the processes.
$ pkill -SIGUSR1 process_name
These examples demonstrate the versatility of the kill command, which can be used to gracefully terminate or forcefully terminate processes, send signals to individual processes or entire process groups, pause and resume processes and more.
Final Thoughts
The kill command causes processes to stop by signaling them to do so. It is typically a built-in shell command, which means that a user’s shell is used to execute the command as opposed to an external executable application. All PID arguments receive a signal when the kill command is used in its first form. TERM signal is the default signal for the kill command (terminate an active process). When a specific SIGNAL (name or number) is supplied, the process behaves in a predetermined manner upon receiving the signal.
You can learn about linux more deeply by clicking the link below
https://linuxiron.com/what-is-linux-a-whole-introduction/
Learn about the other linux commands by clicking the links below
https://linuxiron.com/echo-command-in-linux/
https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/
Execute the kill command after the PID to end a process. Use the top or ps aux commands, as described above, to find a process PID. Execute the command kill 5296 to end a process with the PID 5296.
The kill -9 command instructs a service to quickly terminate by sending a SIGKILL signal. A kill command will be disregarded by an unresponsive program, but a kill -9 command will result in its shutdown.
Taking out 0 does not take out the pid 0. To kill every process in the current group, choose the kill option. With your command, you are terminating every process within the shell’s kill command’s process group ID (GID).
The SIGTERM signal is sent at fifteen (15). The process can catch SIGTERM, release resources and perform a graceful termination. The appropriate command to use when ending a process is “kill -15.” If a signal is not supplied as an argument, ‘kill’ uses fifteen (15) as the default value.
The process or process group provided in the kill command receives the specified signal. The TERM signal is issued if no signal is specified. This signal’s default response is to end the process.