Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to capture signal in shell script

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to capture the signal in the shell script. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Trap literally means trap, but when trap in the shell script, it specifically captures signals such as kill-9, kill, and 15, etc.

1. View all available signals

Trap-l or kill-l is fine.

[root@linux1 ~] # kill-l63) SIGRTMAX-1 64) SIGRTMAX [root@linux1 ~] # trap-l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR111 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM.

2. Common signals are as follows:

When Signal Value Comment ── SIGHUP 1 terminates the process, especially when the terminal exits, the processes in this terminal will be terminated. SIGINT 2 interrupts the process, which is almost equivalent to sigterm. It will release the execution clean-up and release resources as much as possible. Save state, etc. (CTRL+C) SIGQUIT 3 sends a signal from the keyboard to kill (terminate) the process, SIGKILL 9 forcibly kills the process, the signal cannot be captured and ignored, the process will not perform any clean-up behavior after receiving the signal, so the resource will not be released, the state will not save SIGTERM 15 to kill (terminate) the process, which is almost equivalent to the sigint signal, and will be released to execute clean-up as much as possible Release resources, save status, etc. SIGSTOP 19 this signal is a process stop information that can not be captured and ignored. After receiving the signal, it will enter the stopped state SIGTSTP 20. This signal is a process stop signal (CTRL+Z) that can be ignored.

The real signal name is not SIGXXX, but the word after SIG is removed, and each signal has a corresponding code name.

For example, initiate a 1 signal to a process with a PID of 12345

Kill-1 12345kill-HUB 12345kill-SIGHUB 12345

3. Options for trap

Trap-l lists the signals supported by the current system, which have been used above, like the root kill-l.

Trap-p is equivalent to trap. Check the traps that have been set by shell.

You can see that there are three traps in shell by default, which means ignoring the signal 20, 21, and 22.

[root@linux1] # traptrap -''SIGTSTPtrap -' 'SIGTTINtrap -' 'SIGTTOU

4. What do you do after the trap catches the signal?

Ignore the signal

After capturing the signal, do the corresponding processing. The main thing is to clean up some temporary files created by scripts, and then exit.

5. Set a trap that ignores CTRL+C and 15 signals

CTRL signal corresponds to SIGINT 15 signal corresponds to SIGTERM.

[root@linux1 ~] # trap''SIGINT SIGTERM [root@linux1 ~] # traptrap -' 'SIGINTtrap -' 'SIGTERMtrap -' 'SIGTSTPtrap -' 'SIGTTINtrap -' 'SIGTTOU

In this way, the current shell cannot be killed by kill-15

6. Set a trap and print "I've got you" when you catch the-15 signal.

[root@linux1] # trap 'echo "I got you ~" TERM [root@linux1 ~] # traptrap--' 'SIGINTtrap--' echo'I got you''SIGTERMtrap -' 'SIGTSTPtrap -' 'SIGTTINtrap -' 'SIGTTOU

Effect, it is printed when I initiate a kill-15 signal to the current bash

[root@linux1 ~] # echo $8827 [root@linux1 ~] # kill-15 8827 I got you ~ [root@linux1 ~] # kill-15 8827 I got you ~ [root@linux1 ~] # kill-15 8827 I got you ~

7. Set up a script in the script that can ignore CTRL+C and CTRL+Z signals

CTRL+C is a 2 signal, that is, SIGINT

CTRL+Z is the 20 signal, or SIGTSTP.

Script:

The script sleeps for 10 seconds, then prints success, and the script ignores INT and TSTP signals

[root@linux1 ~] # cat trap.shemaking raceme Bash trap''SIGINT SIGTSTPsleep 10echo success

Effect:

CTRL+C can't stop me from sleeping.

[root@linux1 ~] # bash trap.sh ^ C ^ Z ^ C ^ Z ^ Zcc ^ Z ^ Z ^ Z C ^ C ^ C success

8. Set a trap that can clean up the garbage and exit the script immediately when the script is terminated

The script is as follows:

[root@linux1 ~] # cat trap1.shallows racing base trap 'echo trap handing...;rm-rf / tmp/$BASHPID;echo TEMP files cleaned;exit' SIGINT SIGTERM SIGQUIT SIGHUPmkdir-p / tmp/$$/touch / tmp/$$/ {a.. c} .txtsleep 10echo first sleep successsleep 10echo second sleep success

In this way, the script can always clean up the temporary garbage except for the SIGKILL signal (kill-9).

Effect.

At first, it could not be terminated. Later, I executed the trap and found that the previous shell had set a trap to ignore CTRL+C. Just exit the shell and reenter.

[root@linux1 ~] # bash trap1.sh ^ Ctrap handing...TEMP files cleaned

9. The guardian object of the trap

The trap is guarded by the shell process itself and does not guard child processes in the shell environment. However, if it is a signal ignore trap, the entire shell process group is guarded to ignore the given signal.

[root@linux1 ~] # cat trap2.sh #! / bin/bashtrap 'echo trap_handle_time: $(date + "% F% T")' SIGINT SIGTERMecho time_start: $(date + "% F% T") sleep 10echo time_end1: $(date + "% F% T") sleep 10echo time_end2: $(date + "% F% T") # after executing the script The newly opened terminal uses kill-15 to kill it [root@linux1] # killall-s SIGTERM trap2.sh# to check the output [root@linux1] #. / trap2.sh time_start: 2019-08-27 10:43:48trap_handle_time: 2019-08-27 10:43:58time_end1: 2019-08-27 10:43:58time_end2: 2019-08-27 10:44:08

You can see that after the kill execution, the screen does not print trap_handle immediately, but waits for sleep 10 to run before printing. Sleep processes are guarded by omitted trap

Whenever a signal is sent to the shell process, it waits for the currently running command to finish before processing the signal, and then continues to run the script down. (in fact, the signal cannot interrupt the process only when the operation being performed in the shell script is a signal-safe system call, and there is no way to directly know which commands are being executed in the shell system call.)

However, the call to sleep () initiated by the sleep command is signal safe, so during the execution of sleep in the above script, the signal does not interrupt their operation directly, but waits for it to finish before executing the signal processing command.

10. If a trap is set for a signal in shell, when the shell process receives the signal, it will wait for the running command in it to finish before processing the trap

11. CTRL+C and SIGINT are not equivalent. When CTRL+C is pressed at some point, it is sending a SIGINT signal to the entire currently running process group. For shell scripts, SIGINT is sent not only to the shell script process, but also to the currently running process in the script

[root@linux1 ~] # cat trap2.sh #! / bin/bashtrap 'echo trap_handle_time: $(date + "% F% T")' SIGINT SIGTERMecho time_start: $(date + "% F% T") sleep 10echo time_end1: $(date + "% F% T") sleep 10echo time_end2: $(date + "% F% T") # after executing the script Immediately CTRL+ C [root @ linux1 ~] # bash trap2.sh time_start: 2019-08-27 10:20: 53 ^ Ctrap _ handle_time: 2019-08-27 10:20:54time_end1: 2019-08-27 10:20:54time_end2: 2019-08-27 10:21:04

It can be found that after CTRL+C, not only trap but also sleep ends immediately, indicating that CTRL+C not only makes the script process receive the SIGINT signal, but also makes the current process receive the SIGINT signal.

The above is how to capture the signal in the shell script. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report