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 use the trap command of Linux

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to use the trap command of Linux". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The Linux common command trap command is used to specify the action to be taken after the signal is received, and the common use is to complete the cleanup work when the script is interrupted. 1. Linux signal

The Linux system uses signals to communicate with the processes in the system. The common signals of Linux are:

The signal value describes 1SIGHP suspending process 2SIGINT terminating process 3SIGQUIT stopping process 9SIGKILL unconditionally terminating process 15SIGTERM terminating process as far as possible 17SIGSTOP unconditionally stopping process, but not terminating process 18SIGTSTP stopping or pausing process, but not terminating process 19SIGCONT continuing to run stopped process 2. Signal combination key

Ctrl+C key combination will produce SIGINT signal, Ctrl+Z will produce SIGTSTP signal.

3. Trap command

The trap command allows you to specify which Linux signals the shell script will monitor and intercept. The format of the trap command is: trap commands signals.

Demo1

Shell script:

#! / bin/bash# test trap commandtrap "echo 'Sorry! I have trapped Ctrl-C'" SIGINTecho This is a test scriptcount=1while [$count-le 10] do echo "Loop $count" sleep 1 count=$ [$count + 1] doneecho The end.

Running result:

This is a test scriptLoop 1Loop 2 ^ CSorry! I have trapped Ctrl-CLoop 3Loop 4 ^ CSorry! I have trapped Ctrl-CLoop 5Loop 6Loop 7Loop 8 CSorry! I have trapped Ctrl-CLoop 9Loop 10The end.demo2

In addition to capturing the signal in the shell script, it can also be captured when shell exits, adding an EXIT signal after the trap command.

Shell script:

#! / bin/bash# test trap commandtrap "echo Goodbye." EXITecho This is a test scriptcount=1while [$count-le 10] do echo "Loop $count" sleep 1 count=$ [$count + 1] doneecho The end.

Running result:

This is a test scriptLoop 1Loop 2Loop 3Loop 4Loop 5Loop 6Loop 7Loop 8Loop 9Loop 10The end.Goodbye.demo3-- modifies trap operation #! / bin/bash# test trap commandtrap "echo 'Sorry! I have trapped Ctrl-C'" SIGINTcount=1while [$count-le 5] do echo "Loop $count" sleep 1 count=$ [$count + 1] donetrap "echo' Sorry! The trap has been modified.'" SIGINTcount=1while [$count-le 5] do echo "Loop $count" sleep 1 count=$ [$count + 1] doneecho The end.

Running result:

Loop 1Loop 2Loop 3 ^ CSorry! I have trapped Ctrl-CLoop 4Loop 5Loop 1Loop 2Loop 3 ^ CSorry! The trap has been modified.Loop 4Loop 5The end.demo4

Delete the capture, in the form of: trap-- *, such as trap-- SIGINT

This is the end of the content of "how to use the trap command of Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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