The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-24-2009
sagar_evc sagar_evc is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 30
Question shell scripting best practices - trap command

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?
  #2 (permalink)  
Old 08-24-2009
zaxxon's Avatar
zaxxon zaxxon is online now Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,313
For my part I do use the trap command very rarely. If I have need for it, I do but usually I don't since the scripts I write are for admin purposes etc. and they run mostly as root or with an technical account so I don't have to fear any unwanted kills interrupting them.
  #3 (permalink)  
Old 08-24-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,958
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.
  #4 (permalink)  
Old 08-24-2009
sagar_evc sagar_evc is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 30
Is there a signal that is triggered if the script exits due to the lack of disk space?

S
  #5 (permalink)  
Old 08-24-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
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

Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0