How To Use Kill Command In Linux.
On Linux, the “kill” command is used to send a signal to a process, which can be used to kill the process. The signal can be specified as a signal number or as a signal name, and the default signal is the TERM signal, which terminates the process. In this article, we’ll explore the different options and usage of the “kill” command, including how to use it to kill specific processes and how to use it in combination with other commands.
Basic syntax and usage of the Kill command
The basic syntax of the “kill” command is as follows −
Where “signal” is the signal to send to the process and “pid” is the ID of the process you want to kill. For example, the command “kill -9 1234” would send the KILL signal (signal number 9) to the process with process ID 1234, effectively killing it.
It is important to note that the “kill” command can only be used by the process owner or a user with root privileges. This is to prevent unauthorized users from terminating important system processes.
Common signals used with the Kill command
There are several common signals that can be used with the “kill” command, each serving a specific purpose. Some of the more commonly used signals include −
TERM (15) − This is the default signal used by the “kill” command. It requests that the process terminate gracefully, allowing it to clean up any resources or save any data before exiting.
HUP (1) − This signal is used to hang up or disconnect a process. It’s often used to restart a process or to refresh its configuration.
INT (2) − This signal is used to interrupt a process, and is similar to pressing the “CTRL+C” keys on the keyboard.
KILL (9) − This signal is used to immediately terminate a process, without allowing it to clean up or save any data. This signal cannot be ignored by the process.
STOP (19) − This signal is used to stop a process temporarily, allowing it to be resumed later.
Using the Kill Command with Other Commands
The “kill” command can be used in conjunction with other commands to manage processes more effectively on a Linux system. Some examples include −
Using “kill” with “ps” to list running processes: The command “ps -ef | grep [process name]” can be used to list all running processes, and their process IDs, that match the specified process name. The output of this command can then be used with the “kill” command to terminate specific processes.
Using “kill” with “pkill” to terminate processes by name: The “pkill” command can be used to send a signal to all processes that match a specified process name. For example, the command “pkill -9 [process name]” would send the KILL signal to all processes with the specified process name.
Using “kill” with “killall” to terminate all processes by name: The “killall” command can be used to send a signal to all processes with the specified name. For example, the command “killall -9 [process name]” would send the KILL signal to all processes with the specified process name.
The Kill Command with Signals
The kill command can also be used with specific signals to perform different actions on a process. Some common signals that can be used with the kill command include −
SIGINT (2) − Interrupts the process, similar to pressing ctrl+c.
SIGKILL (9) − Forces the process to terminate immediately, without allowing it to clean up or save data.
SIGSTOP (19) − Stops the process, preventing it from running or consuming resources.
SIGCONT (18) − Continues a stopped process.
For example, to stop a process with the PID of 1234, you would enter the following command −
To continue the process, you would use the following command −
Using top command to find the process
To use the top command, simply type “top” in the terminal and press enter. This will display the current running processes on your system, with the most resource-intensive processes listed at the top. You can then use the arrow keys to scroll through the list of processes. To sort the processes by a specific column, such as by memory usage or CPU usage, press the letter “P” for CPU or “M” for memory.
To find the PID of a specific process, you can use the search function by pressing “/” and typing the name of the process you are looking for. Once you have the PID, you can use the “kill” command to terminate the process as discussed in the previous subheadings.
For example, if you want to find the PID of the “firefox” process, you would press “/” and type “firefox” in the top command. It will show the process with firefox in its command line and its corresponding PID.
In this article, we have discussed the different types of kill commands in Linux and their usage. We have also seen how to use the kill command to terminate a process and how to use the killall command to terminate multiple processes at once. Additionally, we have explored the pkill command and its various options that allow for more precise targeting of processes to terminate.