Linux - Traps

     Linux supports both POSIX reliable signals ("standard signals") and POSIX real-time signals.
    A signal is nothing but some sort of inter-process communication (techniques for the exchanging data among multiple threads in one or more processes or commands) in Linux and Unix like operating systems.
    A signal is sent to a process or command in order notify an event that occurred.
    For example, while running a command called 'ls -R /, you may hit CTRL+C (or Break) to cancel command execution. As soon as you hit CTRL+C, a signals called SIGINT (2) sent to indicate interrupt from keyboard. When, SIGINT is sent to ls command, Linux interrupts the process's normal flow of execution. In this example, ls command get terminated.
    However, you can register a signal handler for CTRL+C and take some sort of action like ignore it or display a message on the screen when ls command is interrupted by SIGINT.
    You need to use the trap command to catch signals and handle errors under Linux shell scripts.
    You can send various signals to commands and process. For example, to terminate foreground process you can hit Ctrl+C key combination. To kill background process you can use the kill command and send SIGTERM (terminate command):

kill -TERM pid
kill -TERM 1234

Source: https://bash.cyberciti.biz/guide/Signals