Signals in Shell


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Signals in Shell
# 1  
Old 03-25-2003
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 the following within parent.ksh? (Basically listen for signals 1 - 5 for example and call function 1 if received, and listen for signal 14 and call function 2 if reveived.)

#Begin Parent.ksh

do some logging stuff
...
signal_handler () {
echo "Message Received - Ignoring Signal"
}

cleanup_signal () {
cleanup some stuff
exit $SUCCESS
}
trap signal_handler 1 2 3 4 5
...
do some more stuff
trap cleanup_signal 14
execute child.ksh

do some other stuff

exit SUCCESS

Last edited by google; 03-25-2003 at 05:16 PM..
# 2  
Old 03-25-2003
I'm not exactly following you...but here are a few comments.

To ignore a signal, use:

trap "" 1 2 3 4 5

This will protect subprocesses from signals sent to a process group.

The kernel will send SIGCHILD (18 on HP-UX) automatically to a parent process, but a process is not the same as a script. The ksh process that is running your script will get the SIGCHILD and reap the zombie, but your script is not going to see that SIGCHILD.

To communicate between scripts, you are better off with SIGUSR1 and SIGUSR2.

Signals between scripts never work as well as signals between processes. Remember that a shell script is a collection of processes.

ksh supports signal names rather than numbers. It is better to use the names (even though I still use numbers too). So instead of
trap "" 3
use
trap "" QUIT

"kill -l" (letter ell) gives you a list of the signals.
# 3  
Old 03-25-2003
(In my original post I meant to use signal 14 and not 18) I guess what I trying to ask is can I trap one set of signals and handle them one way and then at the same time trap another signal and handle it differently....something like this

trap "" 1 2 3 4 #ignore these signals
trap SignalHandler 14 # Listen for this signal and if received, call
the SignalHandler function

What we are trying to do is have 2 scripts, a parent and a child. The parent is sitting in a loop until from the time its started until 6pm. The parent executes child who also sits in a loop but wakes every 10 mins to do some process stuff. Parent listens for child and if child is killed or terminates parent restarts child. Wanted parent to ignore most signals, but have child signal back if there was a specific error condition.

Would gladly take criticism/suggestions. Also because of the way a 3rd party vendor has programs alarms set, we cannot use cron to execute - everything is done via a scheduling tool to maintain dependencies.
# 4  
Old 03-26-2003
You should be able to get that to work. Do you need the parent to respond immediately? If not, I would not use signals. I would have the child write a status file and once every 10 minutes the parent would see if the status file exists. If so, it would read it, delete it, and take appropriate action. This is a much wider channel of communication between the processes and it bypasses the problems that signals can cause in scripts.

When the parent launches the child, it should save the pid:
child &
pid=$!

Then to see if the child is still running, use the kill command with signal zero:

if kill -0 $pid ; then
echo child is still running
else
echo child is dead
fi
# 5  
Old 03-26-2003
Perderabo, thanks so much for your help. I am going to experiment with both methods: using signals, and using the temp file.

Out of curiosity, what types of exceptions can I expect if I were to use signals?
# 6  
Old 03-26-2003
Some signals are treated specially by the shell, but this behavior is not documented. The shell may or may not spawn subshells as it runs loops. And commands are usually separate processes. In code like:

sort file | while read filename junk ; do
cp $filename ${filename}.old
done

There are many processes involved in that loop. What does it mean to signal one of them?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with Signals

Hi All, The problem statement is as below: Problem: A process (exe) is getting executed in background. The output of this process is getting logged in a file. After successfully running for some time the process gets terminated. In the log file following is present: ^M[7m Interrupt ^M[27m... (8 Replies)
Discussion started by: Praty.27
8 Replies

2. UNIX for Dummies Questions & Answers

Blocking signals

I know how to add signal to a set. But what if I want to add 2 or 3 signals to the set. I know I can use sigaddset (&set,SIGBUS)....but what if I want to add SIGBUS and SIGALRM at once. Do i have to do it like this.. sigaddset (&set,SIGBUS); sigaddset (&set,SIGALRM); Is there another way to... (0 Replies)
Discussion started by: joker40
0 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. UNIX for Dummies Questions & Answers

threads and signals

can any one give me an example of a concurrency program in threads and signals, i.e how to deliver messages between threads using signals. thanks (0 Replies)
Discussion started by: moe_7
0 Replies

5. Programming

Using Signals

How can use signals in a C program If i want a child program to signal it's parent program that it(child) program has completed the task that it was assigned.:confused: (2 Replies)
Discussion started by: kapilv
2 Replies

6. UNIX for Dummies Questions & Answers

Signals...

(posted this in the scripting forum as well, but figured it should go here) 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 process running, let's... (0 Replies)
Discussion started by: blind melon
0 Replies

7. 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

8. Programming

Threads Signals

Hi In my process there are few threads. Now, lets say all the threads are blocked for some reason or other.. now i read it somewhere that the kernel in this situation sends in some signal which can be caught. please let me know what signal is it and more details about that.. Thanks in... (1 Reply)
Discussion started by: uday_kumar_spl
1 Replies

9. Programming

keypress signals

How-to use keypress action signals in C? Is there any nice select or poll functions for that? (1 Reply)
Discussion started by: Esaia
1 Replies

10. Programming

Signals In HP-UX

does the way of handling, interrupting signals in HP-UX same as that of solaris. If there is difference than what it is.?:confused: (1 Reply)
Discussion started by: kapilv
1 Replies
Login or Register to Ask a Question