- Bash Trap Signals | Capture CTRL+C [Practical Examples]
- Why do we need to trap signals when executing code?
- Understanding signals
- How to use trap in shell scripts (syntax)
- Resetting trap signals
- Ignoring trap signals
- Trap Command Usage Examples
- Example-1: Without trapping any signal
- Example-2: Capture trap signal (Ctrl+C, Ctrl+Z, Ctrl+\)
- Execute bash functions with traps
- Summary
- Further Reading
- linux handle signal
- How are signals handled in Unix?
- How do I see all signals in Linux?
- What is signal function in Linux?
- How do you catch a Linux signal on a script?
- How do I send a signal to Sigterm?
- What signal is Ctrl D?
- What signal is Ctrl C?
- Is Execve a system call?
- How do you send a signal to a process in Linux?
- What signal is Ctrl Z?
- How many Linux signals are there?
Bash Trap Signals | Capture CTRL+C [Practical Examples]
Why do we need to trap signals when executing code?
While your program is running, if you press Ctrl-C or Ctrl-\ , your program terminates as soon as the signal arrives. There are times when you would rather not have the program terminate immediately after the signal arrives. You could arrange to ignore the signal and keep running or perform some sort of cleanup operation before actually exiting the script. The trap command allows you to control the way a program behaves when it receives a signal.
Understanding signals
- A signal is defined as an asynchronous message that consists of a number that can be sent from one process to another, or by the operating system to a process if certain keys are pressed or if something exceptional happens.
- The trap command tells the shell to terminate the command currently in execution upon the receipt of a signal. If the trap command is followed by commands within quotes, the command string will be executed upon receipt of a specified signal.
- The shell reads the command string twice, once when the trap is set, and again when the signal arrives.
- If the command string is surrounded by double quotes, all variable and command substitution will be performed when the trap is set the first time.
- If single quotes enclose the command string, variable and command substitution do not take place until the signal is detected and the trap is executed.
Bash scripts are an integral part of Linux systems. Many manual processes can be automated with bash scripts. You can ensure that the actions to be taken proceed in a way that does not require the intervention of the user. However, users may prevent the script from working properly. For example; They can stop the script from running with the CTRL+C combination. There is no way to prevent this, but you can catch such key combinations in your bash script.
To show all signals and codes, execute the following command on the console;
foc@pardus:/$ 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 16) SIGSTKFLT 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 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
Now let’s look at «How to Capture CTRL+C in Bash»
How to use trap in shell scripts (syntax)
The trap command is used when writing bash scripts on Linux. The standard usage of the trap command is as follows.
trap 'command; command' signal-number trap 'command; command' signal-name
trap 'rm tmp*; exit 1' 0 1 2 15 trap 'rm tmp*; exit 1' EXIT HUP INT TERM
When any of the signals 1 (hangup), 2 (interrupt), or 15 (software termination) arrives, remove all the tmp files and exit.
Resetting trap signals
To reset a signal to its default behavior, the trap command is followed by the signal name or number. Traps set in functions are recognized by the shell that invoked the function, once the function has been called. Any traps set outside the function are also recognized with the function.
- trap INT: Resets the default action for signal 2, SIGINT . The default action is to kill the process when the interrupt key ( Ctrl-C ) is pressed.
- trap 2: Resets the default action for signal 2, SIGINT , which is used to kill a process (i.e., Ctrl-C ).
- trap ‘trap 2’ 2: Sets the default action for signal 2 ( SIGINT ) to execute the command string within quotes when the signal arrives. The user must press Ctrl-C twice to terminate the program. The first trap catches the signal, and the second trap resets the trap back to its default action, which is to kill the process.
Ignoring trap signals
If the trap command is followed by a pair of empty quotes, the signals listed will be ignored by the process. Here, Signals 1 ( SIGHUP ) and 2 ( SIGINT ) will be ignored by the shell process.
trap " " 1 2 or trap "" HUP INT
Trap Command Usage Examples
An interrupt signal ( 2 — SIGINT ) is sent to the system when the user uses the CTRL + C key combination. After this signal, the console drops to a blank line unless otherwise stated.
Example-1: Without trapping any signal
Below is the script that expects the user to enter a value;
#!/bin/bash echo "Enter a value" read input echo Entered value: $input
Run this script as follows;
foc@pardus:~$ ./trap_epl.sh Enter a value
After this command the cursor blinks and waits for a value from the user. If the process is stopped with the CTRL+C key combination instead of entering a value, the terminal switches to a new line.
foc@pardus:~$ ./trap_epl.sh Enter a value ^C foc@pardus:~$
Now let’s capture the CTRL+C key combination with the trap command. Let’s update the script as follows.
Example-2: Capture trap signal (Ctrl+C, Ctrl+Z, Ctrl+\)
In this example, we have setup trap signal to capture different signals. So our shell script will continue to run in while loop and will terminate for known singals. Instead it will capture and ignore them. The script will only terminate if we enter «stop»:
#!/bin/bash trap 'echo -e "\nCtrl-C will not terminate $0."' INT trap 'echo -e "\nCtrl-\\ will not terminate $0."' QUIT trap 'echo -e "\nCtrl-Z will not terminate $0."' TSTP echo "Enter any string after the prompt. When you are ready to exit, type \"stop\"." while true do read -p "Go ahead. > " INPUT if [[ $INPUT == [Ss]top ]] then break fi done
~]# sh /tmp/trap_eg.sh Enter any string after the prompt. When you are ready to exit, type "stop". Go ahead. > ^C Ctrl-C will not terminate /tmp/trap_eg.sh. ^Z Ctrl-Z will not terminate /tmp/trap_eg.sh. ^\ Ctrl-\ will not terminate /tmp/trap_eg.sh. stop
Execute bash functions with traps
If you use a trap to handle a signal in a function, it will affect the entire script, once the function is called. The trap is global to the script.
The syntax remains the same as we showed earlier, just replace the command section with the handler function to be used.
trap 'signal_handler_function_name' SIGNAL LIST
So assuming we want to perform some cleanup when the script receives Ctrl+C then all the code can be placed in a function and that function can be executed for respective trap signals:
#/bin/bash function handler() < echo Hey, received signal : SIGINT echo "I will do some task here and exit" exit 1 ># $$ is a special variable that returns process ID of current # process/script echo My process ID is $$ # handler is the name of the signal handler function for SIGINT signal trap 'handler' SIGINT while true; do sleep 1 done
Here is a sample shell script which will capture the current process PID and trap ctrl+c events. If this trap is received, then handler function will be executed.
~]# sh /tmp/trap_eg.sh My process ID is 6067 ^CHey, received signal : SIGINT I will do some task here and exit
Summary
As a result bash scripts are an integral part of Linux systems. The user’s use of the CTRL+C combination is an inevitable result. Of course, precautions should be taken for this result.
Let’s see what can be done when the CTRL + C combination comes.
- Cache can be cleared in operations such as package installation.
- Using this function it is possible to return to the pre-interruption period. Old configurations can be copied.
- Warnings can be added to the function. After the script is stopped, the steps that the user should take can be written on the screen.
For such reasons, you should add this function to your bash script.
Further Reading
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
linux handle signal
Linux threads call clone with CLONE_SIGHAND; this shares all signal handlers between threads via sharing the current->sig pointer. Delivered signals are unique to a thread. In some operating systems, such as Solaris 7, signals generated as a result of a trap (SIGFPE, SIGILL, etc.) . h thread API.
How are signals handled in Unix?
Signals are, in simple terms, a simple way to communicate one specific thing to to a process, from another process. They default action will be 1 of 5 signals: terminate, terminate and dump core, ignore signal, pause processing, or continue from paused status. .
How do I see all signals in Linux?
Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control.
.
Unix / Linux — Signals and Traps.
Signal Name | Signal Number | Description |
---|---|---|
SIGINT | 2 | Issued if the user sends an interrupt signal (Ctrl + C) |
SIGQUIT | 3 | Issued if the user sends a quit signal (Ctrl + D) |
What is signal function in Linux?
The signal() system call installs a new signal handler for the signal with number signum. The signal handler is set to sighandler which may be a user specified function, or either SIG_IGN or SIG_DFL. . The signals SIGKILL and SIGSTOP cannot be caught or ignored.
How do you catch a Linux signal on a script?
- Send Signal 2 (INT) by typing Control-C.
- Send Signal 3 (QUIT) by typing Control-\.
- Send Signal 23 (STOP) by typing Control-S.
- Send Signal 24 (TSTP) by typing Control-Z.
- Send Signal 25 (CONT) by typing Control-Q.
How do I send a signal to Sigterm?
You can’t send it from a keyboard shortcut, but you can send it from the command line. Based on the man-page for kill, you are able to send a SIGTERM to any process. You would accomplish this by finding your process in the process table (type ps ) and then type kill -15 [pid] .
What signal is Ctrl D?
4 Answers. Ctrl C tells the terminal to send a SIGINT to the current foreground process, which by default translates into terminating the application. Ctrl D tells the terminal that it should register a EOF on standard input, which bash interprets as a desire to exit.
What signal is Ctrl C?
The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Ctrl + C , but on some systems, the «delete» character or «break» key can be used.
Is Execve a system call?
The execve() system call function is used to execute a binary executable or a script. The function returns nothing on success and -1 on error.
How do you send a signal to a process in Linux?
- SIGINT (Ctrl + C) – You know this already. Pressing Ctrl + C kills the running foreground process. This sends the SIGINT to the process to kill it.
- You can send SIGQUIT signal to a process by pressing Ctrl + \ or Ctrl + Y.
What signal is Ctrl Z?
Ctrl + Z is used for suspending a process by sending it the signal SIGSTOP, which cannot be intercepted by the program. While Ctrl + C is used to kill a process with the signal SIGINT, and can be intercepted by a program so it can clean its self up before exiting, or not exit at all.
How many Linux signals are there?
There are 31 standard signals, numbered 1-31. Each signal is named as » SIG » followed by a suffix. Starting from version 2.2, the Linux kernel supports 33 different real-time signals. These have numbers 32-64 but programmers should instead use SIGRTMIN+n notation.
Script
How to Debug a Bash Script like a BossUse options (bash specific)Use breakpoints.Use debug output.Use shopts (bash specific)How do you debug a bash sc.
Automate
Here’s a step by step guide to help you figure out what specific tasks should be automated:Identify the problem you need to solve. It’s easy to think .
Centos
Scientific Linux is Being Discontinued. Brief: Scientific Linux, a distributions focused on scientists in high energy physics field, will not be devel.
Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system