Trap command in CentOS

Source: Internet
Author: User
Tags echo command
The trap command is used to specify the action to be taken after receiving the signal. A common purpose is to complete cleanup when a script program is interrupted. However, this time I met it because the customer has a requirement that a user accessing the server from a terminal will automatically run a command after logging on to the server, such as opening an application (the command is written in. in files such as bashrc), and finally exit and disconnect. during this period, it cannot be allowed to use Ctrl + C to terminate the application and return to the Shell environment. otherwise, security problems may occur. & Nbs

The trap command is used to specify the action to be taken after receiving the signal. A common purpose is to complete cleanup when a script program is interrupted. However, this time I met it because the customer has a requirement that a user accessing the server from a terminal will automatically run a command after logging on to the server, such as opening an application (the command is written in. in files such as bashrc), and finally exit and disconnect. during this period, it cannot be allowed to use Ctrl + C to terminate the application and return to the Shell environment. otherwise, security problems may occur.
Of course, there are many solutions, such as shielding interrupt signals in applications and using chroot access. However, these methods have some restrictions, such as modifying the application and enabling telnet to support the chroot mode (ssh supports the chroot mode. The use of trap is also a better solution.

I. signal
In history, shell always uses numbers to represent signals. new scripts should use the signal names, which are stored in signal contained in the # include command. the SIG prefix must be omitted when the signal name is used in the h header file.
Both kill and trap can see the signal number and its associated name. "Signal" refers to events that are asynchronously sent to a program. By default, they usually terminate the operation of a program.

Reference # trap-l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD
18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN
22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO
30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN + 1
36) SIGRTMIN + 2 37) SIGRTMIN + 3 38) SIGRTMIN + 4 39) SIGRTMIN + 5
40) SIGRTMIN + 6 41) SIGRTMIN + 7 42) SIGRTMIN + 8 43) SIGRTMIN + 9
44) SIGRTMIN + 10 45) SIGRTMIN + 11 46) SIGRTMIN + 12 47) SIGRTMIN + 13
(48) SIGRTMIN + 14 49) SIGRTMIN + 15 50) SIGRTMAX-14 51) SIGRTMAX-13
SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9
(56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59)
(60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63)
64) SIGRTMAX
The appendix contains a description document.

II. Use of trap
1. running format
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 last part is the signal name to be processed.

Trap command signal
It corresponds to three different signal response methods.
First:

Trap "commands" signal-list
When the script receives signals listed in the signal-list, the trap command runs the double quotation mark command.
Second:

Trap signal-list
Trap does not specify any command and receives the default signal operation. the default operation ends the process.
Third:

Trap "" signal-list
The trap command specifies an empty command string to ignore signals. this is what we use.
※Remember that the script program is generally executed in the order from top to bottom, so you must specify the trap command before the part of the code you want to protect.

2. test
According to user requirements, we need to shield several hup int quit tstp signals. Therefore, you can run:

# Trap "" HUP INT QUIT TSTP
At this time, you can try to open a continuous command and interrupt its running, for example:

# Tail-f/var/log/messages
Next, try to use Ctrl + C or Ctrl + \ to interrupt the attempt, and the program will not exit.

3. recovery signal
If you want to recover the program, you can use Ctrl + Z to put the program in the background and then run:

# Trap: HUP INT QUIT TSTP
Then, use ps-ef to check its PID number, bg 1 to run the program, and finally kill it.

4. others
You can also try to run:

# Trap "echo 'hello world'" HUP INT QUIT TSTP
In this way, when you run Ctrl + C or other interruptions, the echo command is automatically run, and the result is the real Hello World string:
Reference # tail-f/var/log/messages
May 18 16:57:54 192.168.228.153 dhcpd: DHCPREQUEST for 192.168.228.221 from 00: 1d: 72: 92: d4: 68 via eth0
May 18 16:57:54 192.168.228.153 dhcpd: DHCPACK on 192.168.228.221 to 00: 1d: 72: 92: d4: 68 via eth0

[Root @ mail ~] # Hello World
※Note: This method does not block interruptions. after you press Ctrl + C and other information, it still uses the default action, that is, exit the program, only one additional command will be run.

III. Appendix
1. interrupt button
Different terminal types and Shell versions have different broken keys, and can even be customized. this can be queried using the stty command:
Reference # stty-
Speed 38400 baud; rows 30; columns 111; line = 0;
Intr = ^ C; quit = ^ \; erase = ^ ?; Kill = ^ U; eof = ^ D; eol = ; Eol2 = ; 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
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
^ Is the abbreviation of Ctrl.

2. signal details
Reference Default action description
SIGHUP
SIGINT terminates process interruption
SIGQUIT creates a CORE file to terminate the process and generates a core file
Invalid command for creating CORE file in SIGILL
SIGTRAP creates CORE file tracking self-traps
Sigbus core file bus creation error
Invalid sigsegv core file segment creation error
An error occurred while creating the CORE file floating point in SIGFPE.
SIGIOT creates CORE file execution I/O Self-traps
SIGKILL terminate the process and kill the process
SIGPIPE terminates a process and writes data to an unread process pipeline.
When SIGALARM terminates the process timer
SIGTERM termination process software termination signal
SIGSTOP stop signal of non-terminal stop process
SIGTSTP stop signal from the process terminal
SIGCONT ignores the signal and continues a stopped process.
SIGURG ignores signal I/O emergency signals
SIGIO can ignore the I/O on the signal descriptor
SIGCHLD ignores the signal and notifies the parent process when the child process stops or exits.
SIGTTOU stops the process background process writing terminal
SIGTTIN stops the process background process Read Terminal
SIGXGPU termination process CPU timeout
The length of the SIGXFSZ termination process file is too long.
SIGWINCH ignores changing signal window size
SIGPROF termination process statistical distribution when timer is used
SIGUSR1 termination process user-defined Signal 1
SIGUSR2 termination process user-defined signal 2
When SIGVTALRM terminates the virtual timer of the process


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.