"Go" Linux Trap

Source: Internet
Author: User
Tags arithmetic

In some cases, we don't want our shell scripts to be interrupted at runtime, such as when we write shell feet
This is set to a user's default shell, so that the user can only do one job after entering the system, such as database backup, I
We do not want users to use CTRL + C and so on into the shell state, do what we do not want to do. This is where the signal is used.
Processing.

Kill-l can list the signal names of the system as follows:
[Email protected]:~/script/test$ kill-l
1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill
5) SIGTRAP 6) SIGABRT 7) Sigbus 8) SIGFPE
9) SIGKILL) SIGUSR1 SIGSEGV) SIGUSR2
Sigpipe) sigalrm SIGTERM) Sigstkflt
SIGCHLD) Sigcont SIGSTOP) SIGTSTP
) (Sigttin) Sigttou () Sigurg) sigxcpu
SIGXFSZ) sigvtalrm sigprof) sigwinch
SIGIO) SIGPWR Sigsys) sigrtmin
(sigrtmin+1) (sigrtmin+2) sigrtmin+3) sigrtmin+4
sigrtmin+5) sigrtmin+6 sigrtmin+7) sigrtmin+8
sigrtmin+9) (sigrtmin+10) sigrtmin+11) sigrtmin+12
sigrtmin+13) (sigrtmin+14) sigrtmin+15) SIGRTMAX-14
SIGRTMAX-13) SIGRTMAX-12-SIGRTMAX-11) SIGRTMAX-10
SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7) SIGRTMAX-6
SIGRTMAX-5) SIGRTMAX-4 () SIGRTMAX-3) SIGRTMAX-2
SIGRTMAX-1) Sigrtmax


Usually we need to ignore the signal has four, namely: HUP, INT, QUIT, TSTP, namely signal 1, 2, 3, 24
Use such statements to make these interrupt signals ignored:
Trap "" 1 2 3 24 or trap "" HUP INT QUIT TSTP
Use Trap:1 2 3 24 or trap HUP INT QUIT TSTP to make it reply to the default value.
Stty-a can be used to list the interrupt signal corresponding to the keyboard, respectively, after executing the above command, run
Tail-f/etc/passwd, and then try to break with the keyboard, try the difference between the two cases (default and ignore).

[Email protected]:~/script/test$ stty-a
Speed 38400 baud; Rows 24; Columns 80; line = 0;
Intr = ^c; Quit = ^/; erase = ^?; Kill = ^u; EOF = ^d; EOL = m-^?; Eol2 = m-^?;
Swtch = m-^?; start = ^q; stop = ^s; Susp = ^z; Rprnt = ^r; Werase = ^w;
Lnext = ^v; flush = ^o; min = 1; Time = 0;
-parenb-parodd CS8 HUPCL-CSTOPB cread-clocal-crtscts
-IGNBRK BRKINT-IGNPAR-PARMRK-INPCK-ISTRIP-INLCR-IGNCR ICRNL Ixon-ixoff
-IUCLC Ixany Imaxbel Iutf8
OPOST-OLCUC-OCRNL Onlcr-onocr-onlret-ofill-ofdel nl0 cr0 tab0 bs0 vt0 ff0
Isig Icanon iexten Echo Echoe echok-echonl-noflsh-xcase-tostop-echoprt
Echoctl Echoke

More convenient is that we can use a trap in the shell to define our own signal handlers, as in C
Like Signal,

such as: Trap "echo ' GO away '" INT

eg

[Email protected]:~/script/test$ cat reboot.sh

#/bin/bash

i=0;
while ((1))
Do
echo "Hello $i"
Sleep 1
((i++))
Echo

echo Trap "bash $ && kill"QUIT
##!trap "bash $ && kill $$" 1
Done
echo haha

Common system Variables:

The name of the current shell program

First to Nineth arguments on the ~ $9 command line

$# the number of arguments on the command line

$* all parameters on the command line

[Email protected] use double quotes to refer to all parameters on the command line, respectively

$$ process identification Number (PID) for the current process

$? Exit status of the previous command

$! Process identification number of the last background process

System variables can only reference cannot be modified! )



[email protected]: ~/script/test$./reboot.sh
Hello 0
20562
trap.sh
Hello 1
20562
trap.sh
Hello 2
Quit
Hello 0
20562
trap.sh
Hello 1
20562
trap.sh
Hello 2
Quit

So ^/can reboot the program ^_^ do not know that Linux restart is not a trap to a certain signal, so ... Of course it is, this script is too east. Time to take a chance. Look at ^_^.

Report:

Introduction to Linux signals
1) SIGHUP This signal is issued at the end of the user terminal connection (normal or abnormal), usually at the end of the terminal control process, notify the same session of the various jobs, then they are no longer associated with the control terminal.
2) SIGINT program termination (interrupt) signal, which is emitted when the user types Intr characters (usually ctrl-c)
3) Sigquit and SIGINT are similar, but are controlled by the Quit character (usually ctrl-/). The process generates a core file when it receives a sigquit exit, similar to a program error signal in this sense.
4) Sigill executed an illegal instruction. This is usually due to an error in the executable file itself or an attempt to execute a data segment. This signal can also be generated when a stack overflows.
5) The SIGTRAP is generated by a breakpoint instruction or other trap instruction. Used by debugger.
6) The SIGABRT program itself discovers the error and calls abort when it is generated.
7) Sigiot is generated on PDP-11 by IoT instructions, on other machines and SIGABRT.
8) Sigbus illegal address, including memory address alignment (alignment) error. Eg: accesses an integer with a length of four words, but its address is not a multiple of 4.
9) The SIGFPE is emitted when a fatal arithmetic operation error occurs. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and divisor 0.
SIGKILL is used to immediately end the operation of the program. This signal cannot be blocked, processed and ignored.
One) SIGUSR1 leave it to the user
SIGSEGV attempts to access memory that is not allocated to itself, or attempts to write data to a memory address that does not have write permissions.
SIGUSR2 leave it to the user
) Sigpipe Broken Pipe
SIGALRM clock timing signal, which calculates the actual time or clock time. The alarm function uses this signal.
SIGTERM program End (terminate) signal, unlike Sigkill, the signal can be blocked and processed. Usually used to ask the program to exit normally. The shell command kill generates this signal by default.
SIGCHLD the parent process will receive this signal at the end of the child process.
Sigcont let a stop (stopped) process continue execution. This signal cannot be blocked. You can use a handler to get the program to do certain work when the stopped state changes to continue. For example, to re-display the prompt
SIGSTOP the execution of the Stop (stopped) process. Notice the difference between it and terminate and interrupt: The process is not over yet, just pause execution. This signal cannot be blocked, processed or ignored.
SIGTSTP stops the process from running, but the signal can be processed and ignored. This signal is emitted when the user types Susp characters (usually ctrl-z)
Sigttin when a background job reads data from a user terminal, all processes in that job receive a sigttin signal. By default, these processes stop executing.
Sigttou is similar to Sigttin, but is received when writing a terminal (or modifying terminal mode).
Sigurg have emergency data or out-of-band data arrive at the socket.
SIGXCPU exceeds the CPU time resource limit. This limit can be read/changed by Getrlimit/setrlimit.
Sigxfsz exceeds the file size resource limit.
() SIGVTALRM virtual clock signal. is similar to SIGALRM, but calculates the CPU time that is consumed by the process.
Sigprof is similar to SIGALRM/SIGVTALRM, but includes the CPU time used by the process and the time of the system call.
sigwinch) When the window size changes.
) The SIGIO file descriptor is ready to start the input/output operation.
SIGPWR Power Failure

The trap command is used to specify what action will be taken after the signal is received, and we will describe the signal in detail later in this book. A common use of the trap command is to complete the cleanup when the script is interrupted. Historically, the shell always used numbers to represent the signal, and the new script should use the name of the signal, which was saved in the Signal.h header file included with the # include command, and the sig prefix needed to be omitted when using the signal name. You can enter the command trap-l at the command prompt to see the signal number and its associated name.

For those unfamiliar with the signal, "signal" refers to events that are sent asynchronously to a program. By default, they usually terminate the operation of a program.

The parameters of the trap command are divided into two parts, the first part is the action to be taken when the specified signal is received, and the latter part is the signal name to be processed.

Keep in mind that scripts are usually interpreted in order from top to bottom, so you must specify the trap command before the part of the code that you want to protect.

If you want to reset the processing condition of a signal to its default value, simply set the command to-. If you want to ignore a signal, set the command to the empty string '. A trap command without parameters will list the current set of signals and their actions.

Table 2-11 lists some of the more important signals that can be captured in the X/open specification (the numbers in parentheses are traditional signal numbers). For more details, please refer to part seventh of the Signal Online manual (man 7 signal).

Table 2-11

Signal

Description

HUP (1)

Hangs, usually caused by a terminal drop or user exit

INT (2)

Interrupt, usually caused by pressing CTRL + C key combination

QUIT (3)

Exit, usually caused by pressing the ctrl+/key combination

ABRT (6)

Abort, usually caused by some serious execution error

ALRM (14)

Alarms, typically used to process timeouts

Term (15)

Terminated, usually sent at system shutdown

"Go" Linux Trap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.