Lecture
Signals - software interrupts. They are used for communication between processes in UNIX and UNIX-like operating systems, such as Linux, Mac OS.
Signals were used with Bell Labs UNIX in 1970 and today officially defined by the POSIX standard. When a signal arrives at a process, the operating system interrupts the normal flow of the process and provides notification.
It does not matter what operating system you are using, you will undoubtedly face the fact that your applications are blocked and refuse to work properly. On Unix, Linux, there is a “kill” command, which you can use to terminate the process immediately, but not only that, but I’ll tell you about this later. In this article, "The kill command on Unix / Linux," I will tell and show the various kill utility commands on ready-made examples.
Kill commands and signals
When you execute the "kill" command, you actually send the signal to the system and instruct you to terminate the operation of the incorrect application robots. There are a total of 60 signals that you can use, but all you really need to know is SIGTERM (15) and SIGKILL (9).
You can view all signals with the command:
1
|
# kill -l
|
On my MacOS:
On CentOS:
On Debian:
I will try to talk about all the signals, but for the beginning we will talk only about the most important and frequently used ones.
The following signals are part of the POSIX standard. Each signal is a macro defined in <signals.h> System header file. They are usually abbreviated, without their SIG prefix; for example, SIGHUP is often simply referred to as HUP.
SIGTERM - This signal requests to stop the process that is running. This signal can be ignored. The process is given time to turn off well. When a program shuts down well, it means that it is given time to save its progress and free up resources. In other words, he is not “forced” to terminate the process.
SIGKILL — The SIGKILL signal causes a process to stop doing its job immediately. The program cannot ignore this signal. Unsaved progress will be lost.
The syntax for "kill".
The command syntax is:
1
|
# kill [signal or option] PID(s)
|
or
1
|
# kill [-s] [-l] %pid
|
The default signal (if not specified) is SIGTERM. When this signal does not help and does not work, you can use the following options for "kill" to force the process to terminate:
1
|
# kill SIGKILL PID
|
Or
1
|
# kill -9 PID
|
where "-9" is a flag that refers to the SIGKILL signal.
If you do not know which need to use the PID of the application you want to get rid of, then run the command:
1
|
$ ps -aux
|
And if you know a specific application (for example, apache), then you can weed out the unnecessary and display all the processes for this service:
1
|
$ ps -aux | grep apache
|
And it will display all running applications along with its PID (s).
For example, to kill an application, I run the following command:
1
|
# kill -9 3629
|
It is also worth noting that you can use several processes at the same time so that they can be “killed”:
1
|
# kill -9 PID1 PID2 PID 3
|
Restart configuration files or restart utilities:
1
|
# kill -1 number_of_PID
|
Signals that are particularly useful include HUP, INT, kill, STOP, CONT, and 0.
The options are as follows:
-s signal_name
A symbolic signal name that specifies a signal to send a non-default signal.
-l [exit_status]
If the operand is not specified, then show the signal names; Otherwise, write the name of the signal corresponding to exit_status.
-signal_name
The symbolic name of the signal that sets the signal to be sent to TERM by default.
-signal_number
A non-negative decimal integer giving the signal to send to TERM by default.
The following PIDs have special meaning:
-1 If the superuser, then transmits a signal to all processes; otherwise broadcasts to all processesbelong-
ing for the user.
Some of the most commonly used signals are:
PKill
The “pkill” command allows you to use extended regular expression patterns and other matching criteria. Instead of using the PID, you can now kill the application by entering the process name. For example, to kill the Firefox browser, simply run the command:
1
|
# pkill Firefox
|
Since it matches the regular expression pattern, you can also enter a part of the process name, for example:
1
|
# pkill fire
|
To avoid “killing” wrong processes, you can make “pgrep -l [process_name]” a list of suitable processes.
The pkill command has many more options, for example, if you specify the “-u” option, it will allow you to specify a username or ID. In this example, we send a TERM signal to all processes belonging to the user 'nobody':
1
|
# pkill -u nobody
|
Killall
Killall uses the process name instead of the PID, and it “kills” all instances of the process with the same name. For example, if you use multiple instances of the Firefox browser, you can kill them all with the command:
1
|
# killall firefox
|
In Gnome, you can restart Nautilus with the command:
1
|
# killall nautilus
|
xkill
Xkill is a graphical way to "kill" the application. When you enter "XKill" in the terminal, the mouse cursor will immediately become "cross." All you have to do is click the “cross” on the guilty app and it will kill the app instantly. If you are interested, you can add a keyboard shortcut to enable the XKill feature.
Sigabrt
This signal sends a signal to the process to interrupt the operation. The ABRT is usually directed to the process itself when it calls the abort () function of the C programming language to signal a crash, but it can be sent from any process, like any other signal.
SIGALRM, SIGVTALRM and SIGPROF
The ALRM, VTALRM and / or PROF signal is sent to the process when the time limit specified when calling the alarm function (for example, setitimer) expires.
ALRM
Sent when the current time or clock is over time.
VTALRM
Sent when processor time used in the process expires.
PROF
Sent when the processor time used by the process and the system on behalf of the process expires.
Sigbus
The BUS signal is sent to the process when it causes a bus error. Conditions that lead to this signal, such as incorrect alignment of memory access or the absence of a physical address.
Sigchld
The CHLD signal is sent to the process when the child process is terminated, interrupted, or resumed after the interruption. One common use of the signal is to give the OS a signal to clean up resources that are used in the process of the child process after it ends without an explicit system call.
Sigcont
The CONT signal instructs the operating system, and gives a signal to restart a process that was previously suspended in STOP mode or a TSTP signal. One of the important features of this signal is the control of work in the Unix shell.
SIGFPE
The FPE signal is sent to the process when it performs erroneous, arithmetic operations, such as division by zero, for example.
SIGHUP
The HUP signal is sent to the process when its controlling terminal is closed. It was originally designed to notify the process after a consecutive line fall (HUP responsible for “hangs”). In modern systems, this signal usually means that the pseudo or virtual terminal control has been closed.
Sigill
ILL is a signal sent to a process when attempting to execute malicious, unknown, or privileged instructions (instructions).
SIGINT
The INT signal is sent to the process from the controlling terminal when the user wishes to interrupt the process. This is usually triggered by pressing Control-C, but on some systems, “delete” or “break”.
Sigkill
The KILL signal is sent to the process so that it stops working immediately. Unlike SIGTERM and SIGINT, this signal cannot be captured or ignored and the receiving process cannot perform any cleanup after receiving this signal.
Sigpipe
The PIPE signal is sent to the process when it tries to write to the pipe without a process connected to the other end.
SIGQUIT
The QUIT signal is sent to the process from its controlling terminal when the user requests a dump process.
SIGSEGV
The SEGV signal is sent to the process when it makes an invalid virtual memory reference or a segmentation failure, that is, when it performs a segmentation violation.
Sigstop
The STOP signal indicates the OS to stop the process for subsequent resumption.
Sigterm
The TERM signal is sent to the process to request its termination. Unlike the “kill” signal, it can be interpreted or ignored in the process. This allows the process to perform “nice” execution to stop the release of resources and save state if necessary. It should be noted that SIGINT is almost identical to SIGTERM.
Sigtstp
The TSTP signal is sent to its controlling terminal process and tells it to temporarily suspend. This is usually initiated by the user pressing Control-Z. Unlike SIGSTOP, this process can register a signal handler or to ignore a signal.
SIGTTIN and SIGTTOU
The TTIN and TTOU signals are sent to the process when it tries to read or write, respectively, with (tty) the terminal in the background. As a rule, this signal can only be received through processes under the control of a work panel; demons do not have control terminals and should never receive this signal.
SIGUSR1 and SIGUSR2
Signals USR1 and USR2 are sent to the process and indicate user-defined conditions.
Sigpoll
A POLL signal is sent in the process when an asynchronous I / O event occurs.
Sigsys
The SYS signal is sent in the process when it misses a bad argument for a system call.
Sigtrap
A TRAP signal is sent to the process when a condition occurs that the debugger asked to be informed — for example, when a particular function is being executed or when a certain value of a variable changes.
Sigurg
A URG signal is sent to the process when the socket is urgent or out of the range of data available for reading.
SIGXCPU
The XCPU signal is sent in the process when it uses the CPU for a time that exceeds a certain specified value set by the user. The arrival of the XCPU signal ensures the process quickly stores all intermediate results and completes well before it is terminated by the operating system using the SIGKILL signal.
Sigxfsz
The XFSZ signal is sent in the process when the file grows (exceeds the specified value) than the maximum allowed value.
SIGRTMIN to SIGRTMAX
RTMIN - RTMAX signals are intended for use in user purposes. They are real time signals.
The following signals are not standardized by POSIX, but are sometimes used on some systems.
Sigemt
An EMT signal is sent in the process when an emulator is interrupted.
Siginfo
An INFO signal is sent in the process when a status request is received from the controlling terminal.
Sigpwr
A PWR signal is sent during the process when the system experiences a power failure.
Siglost
The LOST signal is sent to the process when the file lock is lost.
Sigwinch
The WINCH signal is sent in the process when its controlling terminal changes its size.
Signals can be sent from the keyboard. Several standard defaults are listed below. By default, the shortcut keys for sending interrupt signals can be determined using the stty command.
CTRL-C
Send SIGINT (interrupt). By default, this causes the process to terminate.
CTRL-Z
Send SIGTSTP (Suspend). By default, this causes the process to suspend all operations.
CTRL- \
Send SIGQUIT (Quit). By default, this causes the process to stop immediately and reset the kernel.
CTRL-T
Send SIGINFO (INFO). By default, this causes the operating system to display information about the command. Not supported on all systems.
conclusion
When applications behave incorrectly and cause the system to hang, it is very tempting to restart the computer and start the session again and again. Using these “kill” commands, you can better control the bad behavior of applications that cause or can cause a system crash.
Comments
To leave a comment
LINUX operating system
Terms: LINUX operating system