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

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.

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

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

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?

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?