top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

LINUX SHELL SCRIPT: What is the use of trap command ?

+3 votes
918 views

How trap command can help ?

posted May 27, 2014 by Neeraj Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

When you press the Ctrl+C or Break key at your terminal during execution of a shell program, normally that program is immediately terminated, and your command prompt returned. This may not always be desirable. For instance, you may end up leaving a bunch of temporary files that won't get cleaned up.

Trap command is used to trap these signals.

answer May 27, 2014 by anonymous
0 votes

trap is a built-in feature of a shell which executes the command when the shell receives signals with the exception of SIGKILL and SIGSTOP can not be trapped.

   Signal     Value     Action   Comment
   -------------------------------------------------------------------------
   SIGHUP        1       Term    Hangup detected on controlling terminal
                                 or death of controlling process
   SIGINT        2       Term    Interrupt from keyboard
   SIGQUIT       3       Core    Quit from keyboard
   SIGILL        4       Core    Illegal Instruction
   SIGABRT       6       Core    Abort signal from abort(3)
   SIGFPE        8       Core    Floating point exception
   SIGKILL       9       Term    Kill signal
   SIGSEGV      11       Core    Invalid memory reference
   SIGPIPE      13       Term    Broken pipe: write to pipe with no readers
   SIGALRM      14       Term    Timer signal from alarm(2)
   SIGTERM      15       Term    Termination signal
   SIGUSR1   30,10,16    Term    User-defined signal 1
   SIGUSR2   31,12,17    Term    User-defined signal 2
   SIGCHLD   20,17,18    Ign     Child stopped or terminated
   SIGCONT   19,18,25            Continue if stopped
   SIGSTOP   17,19,23    Stop    Stop process
   SIGTSTP   18,20,24    Stop    Stop typed at tty
   SIGTTIN   21,21,26    Stop    tty input for background process
   SIGTTOU   22,22,27    Stop    tty output for background process

Example of Trap usage in Shell Script

trap 'dialog --msgbox "Script Aborted1" 6 50 ; error exit' 1 2 3 15

In this example of script receives any of the signal 1, 2, 3 or 15, then a popup comes up with the message "Script Aborted1" and the script exits.

answer May 27, 2014 by Salil Agrawal
nice summary
Similar Questions
+2 votes

I have a process in Linux which can have different memory footprint, I want a program or shell script which can print the time of the day when the memory usage is highest for this process.

Can someone help me.

0 votes
+1 vote

I have thousands of html files inside a folder. I want to replace the filename present inside another files. Say for ex:- fileName: 'abcd1234.html' is found inside another file say file2.html. Then I want to remove the last 4 digits of the fileName i.e,. 'abcd1234.html' => 'abcd.htm'.

+1 vote

I have some unique requirement, where I need to do something like this -
1. My script takes the argument as file name which is absolute filename.
2. I need to separate the filename and directory from this absolute file name for the further processing.

Any suggestions -

...