![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how trap the signals in shell scripting | bhas85 | Shell Programming and Scripting | 3 | 08-04-2009 12:28 PM |
| how to use trap command in shell script | girish.batra | UNIX for Advanced & Expert Users | 2 | 03-25-2008 10:49 AM |
| Korn Shell Best Practices | mtravis | Shell Programming and Scripting | 1 | 02-14-2008 03:11 PM |
| Scripting Best Practices | toddjameslane | UNIX for Dummies Questions & Answers | 5 | 03-26-2005 02:09 PM |
| shell scripting my own diff command | axcxe | UNIX for Dummies Questions & Answers | 4 | 12-11-2003 02:20 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
I know there is a command called Code:
trap which can be used to capture the signals from a shell script and redirect the control to a required function (such as a cleanup). My question is - Those of you who have written lot of shell scripts - do you always write a set of trap commands to capture the most frequently occuring abnormal signals and handle them in script manually? Is it a best practice kinda thing to use trap command in a shell cript? |
|
|||||
|
There are 2 signals that I trap in almost every script: SIGUSR1 and 0. I use SIGUSR1 in long-running scripts to print a short status message instead of constantly cluttering the screen, an 0 (a shell-builtin signal meaning EOF) to collect all clean-up stuff in one function, instead of having to worry about it at every possible exit.
|
|
||||
|
I use it to exit foreground monitoring scripts which have a "while true" loop and some background tasks which need killing occasionally. Doubt if it will catch "out of disc space" even when the shell itself is writing to disc. The main use is to action "ctrl/c" from the command line in a controlled manner. Avoid calling the exit routine "EXIT" because it is an undocumented reserved word. Code:
#!/bin/ksh
MYEXIT()
{
## Cleanups go here
exit
}
trap 'MYEXIT' 1 2 3 15
### processing
## last line dropthrough
MYEXIT
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|