shell scripting best practices - trap command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell scripting best practices - trap command
# 1  
Old 08-24-2009
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  
Old 08-24-2009
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  
Old 08-24-2009
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  
Old 08-24-2009
Is there a signal that is triggered if the script exits due to the lack of disk space?

S
# 5  
Old 08-24-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in shell Scripting to display a output from a command

Please find my below requirement and see if you can help me on this. I am looking for a shell script which can provide me the below output. Manuall steps which i am doing now 1) First I source the File $ . ./WC_env.sh 2) Execute the command $ /app/oracle/product/mos/bin/mosotl -url... (2 Replies)
Discussion started by: sudheshpn@gmail
2 Replies

2. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

3. Shell Programming and Scripting

how trap the signals in shell scripting

Hi all, I have the c program that c program has to be in sleep mode. I want write a script the it should trap the signal from the c program. The signals are sighup,sigkill,sigterm,sigchld and then it has to go to sleep. If signal is sigchld it has to do to some function. My question is how to... (3 Replies)
Discussion started by: bhas85
3 Replies

4. Shell Programming and Scripting

How to compare a command line parameter with -- in shell scripting

Hi, I need to check if a parameter provided at the command line is equal to --.How can i do that ? Please help me. Thanks and Regards, Padmini (4 Replies)
Discussion started by: padmisri
4 Replies

5. Shell Programming and Scripting

recommend book for unix command/ shell scripting

Hi, i wish to learn unix commands and shell scripting. platform is solaris. but i am focused more on handy unix commands than system administration. which books do you recommend? (1 Reply)
Discussion started by: nurulamin862
1 Replies

6. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

7. UNIX for Advanced & Expert Users

how to use trap command in shell script

Right now I have implemented autossh between ServerA & ServerB which are sun solaris based. I have made this shell script. I am facing one problem which I am going to discuss now. The problem is when I sftp some files (suppose there is 10 files I have to transfer through sftp) from one server to... (2 Replies)
Discussion started by: girish.batra
2 Replies

8. Shell Programming and Scripting

Korn Shell Best Practices

I am new to ksh scripts (still reading manuals). I need to write an application that reads a multi-line parameter file, builds sql on-the-fly, runs plsql and saves the output in a specific format for further processing. I am looking for anything on Best Practices for building such an... (1 Reply)
Discussion started by: mtravis
1 Replies

9. UNIX for Dummies Questions & Answers

Scripting Best Practices

Hi - I am new to this and was wondering if some of you can help me out. I am just starting to write scripts and need some guidelines on creating scripts. I'm calling them "Best Practices"...what should I do and not do when creating scripts. All I know so far is that I should avoid putting... (5 Replies)
Discussion started by: toddjameslane
5 Replies

10. UNIX for Dummies Questions & Answers

shell scripting my own diff command

Hi I would like to run the diff command and recieve a little different output. I am on a linux machine. I am pretty new to shell scripting. So far my idea has shaped up to this, unworking, script. I would like file1: and file2: instead of the usual > or < output you recieve, diff | sed -e ... (4 Replies)
Discussion started by: axcxe
4 Replies
Login or Register to Ask a Question