Send signal process linux

Linux signals

On Unix-like operating systems such as Linux, signals are software interrupts. They provide a way for the user (or a process) to directly communicate with a process.

Software may be programmed to respond intelligently to a wide array of signals, and certain signals cause processes to behave in a standardized, predefined way at the kernel level.

Description

Process signals were developed as part of UNIX in the 1970s. They are used on all modern Unix-like operating systems, including Linux, BSD, and macOS X.

When a signal is sent to a process, the operating system interrupts the normal flow of the process execution and delivers the notification. If the process has previously registered a way to handle that particular signal, that routine is executed, otherwise the system executes the default signal handler.

Signals can be sent with the kill command, which is named for its default signal (SIGKILL) that instructs the OS to forcefully terminate a process before doing anything else.

Signal names are commonly abbreviated without their SIG prefix, e.g., «KILL», including in the command arguments of kill.

Finding system-specific signals

Signals are defined in the system library signal.h. To view the signals used by your operating system, open a terminal and run man signal or man 7 signal.

Signals in Linux

Signal Number Description Standard
SIGHUP 1 The HUP signal is sent to a process when its controlling terminal is closed. It was originally designed to notify a serial line drop (HUP stands for «Hang Up»). In modern systems, this signal usually indicates the controlling pseudo or virtual terminal is closed. POSIX
SIGINT 2 The INT signal is sent to a process by its controlling terminal when a user wants to interrupt the process. This signal is often initiated by pressing Ctrl + C , but on some systems, the «delete» character or «break» key can be used. ANSI
SIGQUIT 3 The QUIT signal is sent to a process by its controlling terminal when the user requests that the process perform a core dump. POSIX
SIGILL 4 Illegal instruction. The ILL signal is sent to a process when it attempts to execute a malformed, unknown, or privileged instruction. ANSI
SIGTRAP 5 Trace trap. The TRAP signal is sent to a process when a condition arises that a debugger is tracing — for example, when a particular function is executed, or when a particular variable changes value. POSIX
SIGABRT,
SIGIOT
6 Abort process. ABRT is usually sent by the process itself, when it calls the abort() system call to signal an abnormal termination, but it can be sent from any process like any other signal. SIGIOT is a synonym for SIGABRT. (IOT stands for input/output trap, a signal which originated on the PDP-11.) 4.2 BSD
SIGBUS 7 The BUS signal is sent to a process when it causes a bus error, such as an incorrect memory access alignment or non-existent physical address. In Linux, this signal maps to SIGUNUSED, because memory access errors of this kind are not possible. 4.2 BSD
SIGFPE 8 Floating point exception. The FPE signal is sent to a process when it executes erroneous arithmetic operations, such as division by zero. ANSI
SIGKILL 9 Forcefully terminate a process. With STOP, this is one of two signals which cannot be intercepted, ignored, or handled by the process itself. POSIX
SIGUSR1 10 User-defined signal 1. This is one of two signals designated for custom user signal handling. POSIX
SIGSEGV 11 The SEGV signal is sent to a process when it makes an invalid virtual memory reference, or segmentation fault, i.e., when it performs a segmentation violation.
SIGUSR2 12 User-defined signal 2. This is one of two signals designated for custom user signal handling. POSIX
SIGPIPE 13 The PIPE signal is sent to a process when it attempts to write to a pipe without a process connected to the other end. POSIX
SIGALRM 14 The ALRM signal notifies a process that the time interval specified in a call to the alarm() system function has expired. POSIX
SIGTERM 15 The TERM signal is sent to a process to request its termination. Unlike the KILL signal, it can be caught and interpreted or ignored by the process. This signal allows the process to perform nice termination releasing resources and saving state if appropriate. It should be noted that SIGINT is nearly identical to SIGTERM. ANSI
SIGSTKFLT 16 Stack fault. Maps to SIGUNUSED in Linux.
SIGCHLD 17 The CHLD signal is sent to a process when a child process terminates, is interrupted, or resumes after being interrupted. One common usage of the signal is to instruct the operating system to clean up the resources used by a child process after its termination without an explicit call to the wait system call. POSIX
SIGCONT 18 Continue executing after stopped, e.g., by STOP POSIX
SIGSTOP 19 The STOP signal instructs the operating system to stop a process for later resumption. This is one of two signals, along with KILL that cannot be intercepted, ignored, or handled by the process itself. POSIX
SIGTSTP 20 The TSTP signal is sent to a process by its controlling terminal to request it to stop temporarily. It is commonly initiated by the user pressing Ctrl + Z . Unlike SIGSTOP, this process can register a signal handler for or ignore the signal. POSIX
SIGTTIN 21 The TTIN signal is sent to a process when it attempts to read from the tty while in the background. This signal can be received only by processes under job control. Daemons do not have controlling terminals and should never receive this signal. POSIX
SIGTTOU 22 TTOU signal is sent to a process when it attempts to write from the tty while in the background. The compliment to TTIN. POSIX
SIGURG 23 The URG signal is sent to a process when a socket has urgent or out-of-band data available to read. 4.2 BSD
SIGXCPU 24 The XCPU signal is sent to a process when it has used up the CPU for a duration that exceeds a certain predetermined user-settable value. The arrival of an XCPU signal provides the receiving process a chance to quickly save any intermediate results and to exit gracefully, before it is terminated by the operating system using the SIGKILL signal. 4.2 BSD
SIGXFSZ 25 The XFSZ signal is sent to a process when it grows a file larger than the maximum allowed size. 4.2 BSD
SIGVTALRM 26 Virtual alarm clock. May be sent by the alarm() system call. By default, this signal kills the process, but it’s intended for use with process-specific signal handling. 4.2 BSD
SIGPROF 27 Profiling alarm clock. Indicates expiration of a timer that measures CPU time used by the current process («user» time), and CPU time expended on behalf of the process by the system («system» time). These times may be used to implement code profiling facilities. By default, this signal terminates the process, but it’s intended for use with process-specific signal handling. 4.2 BSD
SIGWINCH 28 Window change. The WINCH signal is sent to a process when its controlling terminal changes size, for instance if you resize it in your window manager. 4.3 BSD, Sun
SIGIO,
SIGPOLL
29 Input/output is now possible. SIGPOLL is a synonym for SIGIO, and in Linux its behavior is identical to SIGURG. 4.2 BSD
SIGPWR,
SIGLOST
30 Power failure. The PWR signal is sent to a process when the system detects a power failure. SIGLOST is a synonym for SIGPWR. System V
SIGUNUSED,
SIGSYS
31 Unused signal. This signal is provided for compatibility reasons, for example when porting software from an operating system with different or unsupported signals in Linux. In Linux, SIGSYS is a synonym for SIGUNUSED. System V r4
Читайте также:  Где включить блютуз windows 10

Signals not supported by Linux

The following signals may be used by other systems, such as BSD, but are interpreted as SIGUNUSED in Linux.

SIGEMT The EMT signal is sent to a process when an emulator trap occurs. Unused in Linux.
SIGINFO The INFO signal is sent to a process when a status request is received from the controlling terminal. Unused in Linux
SIGLOST The LOST signal is sent to a process when a file lock is lost. Unused in Linux.
SIGSYS The SYS signal is sent to a process when it passes a bad argument to a system call. Unused in Linux.

Sending signals from the keyboard

Signals may be sent from the keyboard. Several standard defaults are listed below. Default key combinations for sending interrupt signals can be defined with the stty command.

Ctrl-C Send SIGINT (Interrupt). By default, this causes a process to terminate.
Ctrl-Z Send SIGTSTP (Suspend). By default, this causes a process to suspend all operation.
Ctrl-\ Send SIGQUIT (Quit). By default, this causes a process to terminate immediately and dump the core.
Ctrl-T Send SIGINFO (Info). By default, this causes the operating system to display information about the command. Not supported on all systems.

Real-time signals

Real-time signals are a set of signals with no predefined purpose, for programmers to use as they want in their software. Two signal names, SIGRTMIN and SIGRTMAX, define the minimum and maximum signal numbers of the real-time signals. For example, the programmer may use the signal number as SIGRTMIN+3 to refer to the fourth real-time signal number.

Examples: sending signals

The kill command sends signals to processes. Your shell may have a built-in version of kill, which supersedes the version installed at /bin/kill. The two versions have slightly different options, but basic functions are the same. The following examples may run using either version of kill.

Читайте также:  Windows 10 pro home rtm

The process to be signaled is referred to by PID (process ID). If you’re not sure of the process ID, you can find it with the ps command, for example ps -aux.

Send the KILL signal to the process with PID 1234.

Kill three processes: PIDs 123, 456, and 789.

Send signal number 15 (TERM) to processes 1234 and 5678.

Same as the previous command.

List all available signals. Example output:

The special process ID -1 refers to all processes other than kill and the system root process. This command attempts to kill (-9) every possible process (-1) on the system. For more information, see the documentation of kill, linked below.

kill — End a process.
stty — Change terminal line settings.

Источник

Sending signal to Processes

You can send various signals to commands / process and shell scripts using the, pkill command, kill command, and killall command.

Contents

kill — send a signal to a process

The default signal for kill is TERM. To list available signals, enter:

kill command Examples

The kill command can send all of the above signals to commands and process. However, commands only give response if they are programmed to recognize those signals. Particularly useful signals include:

  1. SIGHUP (1) — Hangup detected on controlling terminal or death of controlling process.
  2. SIGINT (2) — Interrupt from keyboard.
  3. SIGKILL (9) — Kill signal i.e. kill running process.
  4. SIGSTOP (19) — Stop process.
  5. SIGCONT (18) — Continue process if stopped.

To send a kill signal to PID # 1234 use:

killall — kill processes by name

killall sends a signal to all processes running any of the specified commands . If no signal name is specified, SIGTERM is sent. To terminate all firefox process (child and parent), enter:

To send a KILL signal to firefox, enter:

pkill — kill process

The pkill command is another command with additional options to kill process by its name, user name, group name, terminal, UID, EUID, and GID. It will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout. To send a kill signal to php-cgi process, enter:

Читайте также:  Браузер с прокси сервером windows

The above example will kill all users php-cgi process. However, -u option will kill only processes whose effective user ID is set to vivek:

Make sshd reread its configuration file, enter:

Источник

kill(2) — Linux man page

kill — send signal to a process

Synopsis

Description

The kill() system call can be used to send any signal to any process group or process.

If pid is positive, then signal sig is sent to the process with the ID specified by pid.

If pid equals 0, then sig is sent to every process in the process group of the calling process.

If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init), but see below.

If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.

If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

For a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of the sending process must equal the real or saved set-user-ID of the target process. In the case of SIGCONT it suffices when the sending and receiving processes belong to the same session.

Return Value

On success (at least one signal was sent), zero is returned. On error, -1 is returned, and errno is set appropriately.

Errors

An invalid signal was specified.

The process does not have permission to send the signal to any of the target processes.

The pid or process group does not exist. Note that an existing process might be a zombie, a process which already committed termination, but has not yet been wait(2)ed for.

Conforming To

SVr4, 4.3BSD, POSIX.1-2001.

Notes

The only signals that can be sent to process ID 1, the init process, are those for which init has explicitly installed signal handlers. This is done to assure the system is not brought down accidentally.

POSIX.1-2001 requires that kill(-1,sig) send sig to all processes that the calling process may send signals to, except possibly for some implementation-defined system processes. Linux allows a process to signal itself, but on Linux the call kill(-1,sig) does not signal the calling process.

POSIX.1-2001 requires that if a process sends a signal to itself, and the sending thread does not have the signal blocked, and no other thread has it unblocked or is waiting for it in sigwait(3), at least one unblocked signal must be delivered to the sending thread before the kill() returns.

Linux notes Across different kernel versions, Linux has enforced different rules for the permissions required for an unprivileged process to send a signal to another process. In kernels 1.0 to 1.2.2, a signal could be sent if the effective user ID of the sender matched that of the receiver, or the real user ID of the sender matched that of the receiver. From kernel 1.2.3 until 1.3.77, a signal could be sent if the effective user ID of the sender matched either the real or effective user ID of the receiver. The current rules, which conform to POSIX.1-2001, were adopted in kernel 1.3.78.

In 2.6 kernels up to and including 2.6.7, there was a bug that meant that when sending signals to a process group, kill() failed with the error EPERM if the caller did not have permission to send the signal to any (rather than all) of the members of the process group. Notwithstanding this error return, the signal was still delivered to all of the processes for which the caller had permission to signal.

Источник

Оцените статью