how trap the signals in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how trap the signals in shell scripting
# 1  
Old 08-04-2009
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 write the script to trap the signal.
I know the trap commnad but i dont know how to trap the signals.
Please help me in this issue.
Please let me if you didnt understand my question.
Thanks in advance.
Vijay,
# 2  
Old 08-04-2009
You cannot trap all signals - some are processed regardless of the the trap
Code:
kill -l

shows the names and numbers of all of the signals you system uses. There are differences between unixes in what this display shows.

Suppose you want to trap control/c - SIGINT, signal =2 on my system.
You can use either the number 2 or the name - drop the "SIG" part and you can use INT

Code:
#!/bin/ksh
trap 'echo "control/c ignored" ' INT

[/code]
# 3  
Old 08-04-2009
hi

Hi thanks for ur valuable reply.
# 4  
Old 08-04-2009
How to test which signal you can catch with shell ? Here is example script:
Code:
#!/bin/ksh  # or bash
sig=0
while (( sig < 30 ))
do
        trap "echo sig:$sig " $sig
        (( sig+=1 ))
done

while true
do
        echo "my pid is $$"
        echo "use: kill -N $$"
        echo " - N is 0-29"
        sleep 60
done

Start this script in background and then use kill to send different signal for this bg process.

---------- Post updated at 06:28 PM ---------- Previous update was at 05:05 PM ----------

Basic trap handling, exit = signal EXIT


Code:
realexit() 
{
   echo "int EXIT has done"
}

ctrlc() 
{       
   echo "int INT (ctrl-C)"
   echo "end this script"
   exit
}
        
trap 'realexit' EXIT
trap 'ctrlc' INT 
trap ':' HUP QUIT  # nop - do nothing - process continue

while true
do
    date
    echo "proc:$$"
    sleep 30
done

Start this script in background and send signals using kill
kill -HUP $!
kill -INT $!
kill -QUIT $!
...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reg trap signals

let LIMIT=50 function check { if ]; then print LIMIT OK else print "LIMIT changed!" fi } trap check DEBUG print $LIMIT LIMIT=$((LIMIT + 30)) trap - DEBUG Can anyone tell me how debugging is accomplished?? (4 Replies)
Discussion started by: Karthick N
4 Replies

2. Shell Programming and Scripting

Help with trap and signals

I am having issues with trap not working inside a script. I am currently trying this on a Knoppix system V 5.1. What I would like to happen is when I press control c, a message gets echoed and the script is ended. For example: #! /bin/bash trap "echo CTRL c was pressed ; break" SIGINT... (11 Replies)
Discussion started by: Basherrr
11 Replies

3. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

4. Shell Programming and Scripting

shell scripting best practices - trap command

I know there is a command called 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... (4 Replies)
Discussion started by: sagar_evc
4 Replies

5. Shell Programming and Scripting

Question using signals in my own shell..

Wasn't really sure where to put this, since I'm using C in UNIX, but I am making my own shell... so, what's going on is this: For our program, we had to create our own shell, and if the user pressed ctrl-c just at the cmdline, then this signal would be ignored, but if there is a foreground... (1 Reply)
Discussion started by: blind melon
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

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

9. UNIX for Advanced & Expert Users

Signals in Shell

Using Korn Shell on HP-UX 11.x Question about signals between two shells. First shell is the parent that kicks off the second shell. The first shell is going to ignore a standard set of signals, but I want the second shell to signal back to its parent if event X happens. Can I do something of... (5 Replies)
Discussion started by: google
5 Replies

10. UNIX for Advanced & Expert Users

how to trap signals.

hi!!, i wanna trap all Signal 10, 11, 15 generated by any process running on my server irrespective of the user and wanna write to a log file. Any ideas? Do this script need to be a root process?? :cool: (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question