徐小玉的博客。
分类: LINUX
2020-01-14 11:15:42
What is the kill command in UNIX?
The kill command is a command line utility to for terminating processes. It is normally a shell builtin meaning the command is called from a users shell rather than an external executable program. By default the kill command will send a TERM signal to a process allowing the process to perform any cleanup operations before shutting down. The kill command also supports sending any other signal to a process. The kill command is used primarily to terminate or restart processes.
SYNOPSIS
kill [-s signal_name] pid ...
kill -l [exit_status]
kill -signal_name pid ...
kill -signal_number pid ...
DESCRIPTION
The kill utility sends a signal to the processes specified by the pid operands.
Only the super-user may send signals to other users' processes.
The options are as follows:
-s signal_name
A symbolic signal name specifying the signal to be sent instead of the default TERM.
-l [exit_status]
If no operand is given, list the signal names; otherwise, write the signal name corresponding to exit_status.
-signal_name
A symbolic signal name specifying the signal to be sent instead of the default TERM.
-signal_number
A non-negative decimal integer, specifying the signal to be sent instead of the default TERM.
The following PIDs have special meanings:
-1 If superuser, broadcast the signal to all processes; otherwise broad-cast to all processes belonging to the user.
Some of the more commonly used signals:
1 HUP (hang up)
2 INT (interrupt)
3 QUIT (quit)
6 ABRT (abort)
9 KILL (non-catchable, non-ignorable kill)
14 ALRM (alarm clock)
15 TERM (software termination signal)
EXIT STATUS
The kill utility exits 0 on success, and >0 if an error occurs.
EXAMPLES
Terminate the processes with PIDs 142 and 157:
kill 142 157
Send the hangup signal (SIGHUP) to the process with PID 507:
kill -s HUP 507
How to kill a process
To kill, or terminate a process first find out the process identifier number or PID of the process to be killed, then pass the PID number to the kill command. In the following example suppose that we are running the mutt terminal email program and that we wish to terminate it. To find the process identifier the ps command is used along with grep to find the PID.
ps -e | grep mutt
ps -e | grep mutt
17146 pts/1 00:00:00 mutt
The same may also be achieved by running the pgrep command. This will return the process identifier or identifiers for the search pattern.
pgrep mutt
17146
Once the PID for the mutt program is known it may be used with the kill command.
kill 17146
This sends a TERM signal to the process indicating it should be terminated. When a process receives a TERM it acts as a request to terminate the running process. A UNIX process may catch a TERM and handle termination gracefully such as releasing resources or saving state.
It is also possible to use pkill to achieve the same result.
pkill mutt
How to (really) kill a process
If a process does not respond to a TERM signal the KILL signal may be used. The KILL signal cannot be ignored by UNIX processes and the process is killed immediately. Note that this does not allow the process to perform any cleanup when shutting down the process. To send a process a signal other than TERM use the -s option followed by the name of the signal.
kill -s KILL 17146
Using the signal number is more commonly used and is equivalent.
kill -s 9 17146
Some shell built-ins also support the following syntax for even more brevity.
kill -9 17146
Any of these signals may be sent to a process using either the number or name of a signal. The following are equivalent ways to send a process a HUP signal.
kill -s HUP 17146
kill -s 1 17146
kill -HUP 17146
kill -1 17146
kill -l :To display all the available signals you can use below command option:
reference: man page and: