In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use the trap command in Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The commonly used Linux command trap command is mainly used to specify the action to be taken after receiving the signal and to perform cleaning work when the script is interrupted.
Trap captures signals and other events and executes commands.
Summary trap [- lp] [[arg] signal_spec.] Option-l prints the signal name and the number corresponding to the signal name. -p displays the trap command associated with each signal. Parameter arg: the command that is executed when a signal is received.
Signal_spec: the signal name or the number corresponding to the signal name.
The return value returns 0 if the expression execution is successful, and 1 if the parameter signal_spec does not specify a valid value.
The signal signal is an inter-process communication mechanism, which provides an asynchronous software interrupt to the application, so that the application has the opportunity to receive commands (that is, signals) sent by other program live terminals. When an application receives a signal, there are three ways to handle it: ignore, default, or capture. When the process receives a signal, it checks the processing mechanism of the signal. If it is SIG_IGN, it ignores the signal; if it is SIG_DFT, it uses the default processing action of the system, usually terminating the process or ignoring the signal; if a handler function (capture) is specified for the signal, it will interrupt the task being executed by the current process and execute the signal handler instead, and then continue to execute the interrupted task after returning.
In some cases, we do not want our shell script to be interrupted at run time, for example, we wrote the shell script as the default shell of a user, so that this user can only do a certain job after entering the system, such as database backup, we do not want users to use methods such as Ctrl+C to enter the shell state to do things we do not want to do. This uses signal processing.
Here are some common signals you may encounter:
Signal name signal number description SIGHUP1 this signal is sent at the end of the user terminal connection (normal or abnormal), usually at the end of the terminal control process, notifying each job in the same session, when they are no longer associated with the control terminal. When logging in to Linux, the system assigns a terminal (Session) to the logged-in user. All programs running on this terminal, including foreground process group and background process group, generally belong to this Session. When the user logs out of Linux, the foreground process group and the process with output to the terminal in the background will receive the SIGHUP signal. The default operation of this signal is to terminate the process, so processes with terminal output in the foreground and background will be aborted. For daemons that are disconnected from the terminal, this signal is used to tell it to reread the configuration file. The SIGINT2 program termination (interrupt) signal is sent when the user types Ctrl+C. SIGQUIT3 is similar to SIGINT, but is controlled by the QUIT character (usually Ctrl /). The process generates a core file when it receives a SIGQUIT exit, which is similar to a program error signal in this sense. SIGFPE8 is emitted when a fatal arithmetic error occurs. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflows and divisor 0. SIGKILL9 is used to immediately end the running of the program. This signal cannot be blocked, processed or ignored. SIGALRM14 clock timing signal, which calculates the actual time or clock time. The alarm function uses this signal. SIGTERM15 Program end (terminate) signal, unlike SIGKILL, can be blocked and processed. It is usually used to require the program to exit normally; the kill command generates this signal by default. Example when shell receives the HUP INT PIPE QUIT TERM commands, the currently executed program executes exit 1.
[root@pc root] $trap "exit 1" HUP INT PIPE QUIT TERM1 Clean temporary Files the following shows how to delete the file and exit if someone tries to abort the program from the terminal:
Trap "rm-f $WORKDIR/work1 $WORKDIR/dataout; exit" 2 executes the shell program, and if the program receives a signal of 2, the two files (work1 and dataout) will be deleted automatically.
Add signal 1 SIGHUP:
$trap "rm $WORKDIR/work1 $WORKDIR/dataout; exit" 1 22 ignore signal if the command listed in the trap is empty, the specified signal will be ignored when it is received:
$trap''2 ignores multiple signals:
$trap''1 2 3 153 reset trap when you change the action taken after receiving the signal, you can omit the first parameter to reset to the default behavior.
$trap 12 Note trap-l is equivalent to executing kill-l.
To send a signal, please check the kill command.
This command is a bash built-in command. For help, please see the help command.
This is the end of the article on "how to use trap commands in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.