[
177
]
To better understand what the
kill
command does, you first will need to understand
the basics of
Linux Signals
. Signals are used by both administrators and developers
and can be sent to a process either by the kernel, another process, or manually with
a command. A signal instructs the process of a request or change, and in some cases,
to completely terminate. An example of such a signal is
SIGHUP
, which tells processes
that their controlling terminal has exited. One situation in which this may occur is
when you have a terminal emulator open, with several processes inside it running. If
you close the terminal window (without stopping the processes you were running),
they'll be sent the
SIGHUP
signal, which basically tells them to quit (essentially, it
means the shell quit or hung up).
Other examples include
SIGINT
(where an application is running in the foreground
and is stopped by pressing
Ctrl
+
c
on the keyboard) and
SIGTERM
, which, when sent
to a process, asks it to cleanly terminate. Yet another example is
SIGKILL
, which
forces a process to terminate uncleanly. In addition to a name, each signal is also
represented by a value, such as
15
for
SIGTERM
and
9
for
SIGKILL
. Going over each
of the signals is beyond the scope of this chapter (the advanced topics of signals are
mainly only useful for developers), but you can view more information about them
by consulting the man page if you're curious:
man 7 signal
For the purposes of this section, the two types of signals we are most concerned
about are
SIGTERM(15)
and
SIGKILL(9)
. When we want to stop a process, we send
one of these signals to it, and the
kill
command allows us to do just that. By default,
the
kill
command sends signal
15
(
SIGTERM
), which tells the process to cleanly
terminate. If successful, the process will free its memory and gracefully close. With
our previous example
kill
command, we sent signal
15
to the process, since we
didn't clarify which signal to send.
Terminating a process with
SIGKILL(9)
is considered an extreme last resort. When
you send signal
9
to a process, it's the equivalent of ripping the carpet out from
underneath it or blowing it up with a stick of dynamite. The process will be force-
closed without giving it any time to react at all, so it's one of those things you
should avoid using unless you've literally tried absolutely everything you can think
of. In theory, sending signal
9
can cause corrupted files, memory issues, or other
shenanigans to occur. As for me, I've never actually run into long-term damage
to software from using it, but theoretically it can happen, so you want to only use
it in extreme cases. One case where such a signal may be necessary is regarding
defunct
or
Zombie process
in a situation where they don't close on their own.
These processes are basically dead already and are typically waiting on their parent
processes to reap them.
Controlling and Managing Processes
Do'stlaringiz bilan baham: |