Strange SIGINT propagation between Parent/Child sh scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange SIGINT propagation between Parent/Child sh scripts
# 1  
Old 10-06-2009
Strange SIGINT propagation between Parent/Child sh scripts

Good day,

I am trying to add signal handling capabilities to some of my scripts. Unfortunately, I am having some difficulty with the manner in which signals are propagated between parent/child processes. Consider the following example:

I have the following "parent" script:

Code:
#!/usr/bin/sh

child_id=0

SIGINT_handler()
{
     echo "SIGINT caught in Parent"
     if [  $child_id  -ne 0 ]
         then
            echo Parent sendingi: kill -n 2 $child_id
            kill -n 2 $child_id
     fi
}


echo parent running
trap 'echo parent exiting; exit' 0
trap 'SIGINT_handler' 2                            # Pass Signal 2 to child but don't die
./child &
child_id=$!
echo 'child_id = ' $child_id

# Wait until child-process exits
wait $child_id
WAIT_STATUS=$?
echo Wait Status recorded when parent continues: $WAIT_STATUS

echo Parent Still Running after the child exits!
sleep 1000

which calls the following "child" script (as a background process):

Code:
#!/usr/bin/sh

echo child started. pid is $$
#trap 'echo child exiting; exit 0' 0
trap 'echo child got signal 2; exit 0' 2
sleep 1000

When I execute the "parent" process with a ">./parent",
I observe that the expected behavior. I then send a
SIGINT to the "parent" via a ">kill -s SIGINT <ppid>" and
get the following screen output:

Quote:
SIGINT caught in Parent
Parent sending: kill -n 2 20944
Wait Status recorded when parent continues: 130
Parent Still Running after the child exits!
When I look at the processes that are still active, I see
that the child (and its sleep) has not been killed at all -
all that happened was that the sleep-process of the parent
was activated.

I would appreciate it if anybody has some idea what is happening
here.
# 2  
Old 10-06-2009
'When I execute the "parent" process with a ">./parent",' This does not work for me but . parent does run it. Similarly I changed './child &' to '. child &' and got the following output: -

TX5XN:/home/brad/wip/signals>. parent
parent running
child started. pid is 3698
[1] 7191
child_id = 7191

a ps -ef gave: -

brad 7191 3698 0 20:04 pts/0 00:00:00 ksh

CTRL C of parent produced this output: -

^Cchild got signal 2
Wait Status recorded when parent continues: 0
Parent Still Running after the child exits!

"child got signal 2" is the message from the child's signal handler, not the parent; at this point a ps shows that the child is gone.

A further CTRL C gives: -

^CSIGINT caught in Parent
Parent sending: kill -n 2 7191
kill: 7191: no such process
SIGINT caught in Parent
Parent sending: kill -n 2 7191
kill: 7191: no such process

Note that the handler is called twice.

When I run your code with the line './child &' in it I get the output: -

TX5XN:/home/brad/wip/signals>. parent
parent running
ksh: .: line 19: ./child: not found
[1] 7398
child_id = 7398
Wait Status recorded when parent continues: 127
Parent Still Running after the child exits!
^CSIGINT caught in Parent
Parent sending: kill -n 2 7398
kill: 7398: no such process
SIGINT caught in Parent
Parent sending: kill -n 2 7398
kill: 7398: no such process

Are you sure you actually ran the child process or am I missing something? I don't have much experience of signal handlers?
# 3  
Old 10-07-2009
Hi steadyonabix,

Thanks for your quick reply! I think the "./" works when you make the scripts executable. What I did, was to make each file "parent" and "child" executable with chmod, i.e.

> chmod "+x" parent

and then the same for the child. Then the "./parent" and "./child &" commands should work.

Also, you are using the korn shell. I am using the bash, or sh. (I am not sure which one of the 2 - I am fairly new to Unix).
# 4  
Old 10-15-2009
I have reformulated my original question in the form of 3 new small sh scripts to illustrate exactly what I am struggling with, w.r.t parent/child process behavior:

Consider a "parent" process:
Code:
#!/bin/sh

sleep 600 &
#./child1 &
PID1="$!"

sleep 600 &
#./child2 &
PID2="$!"

trap "kill $PID1 $PID2" exit  INT
wait

I made the script executable with ">chmod "+x" parent" and executed it in a bash environment, via ">./parent".

I then do a "ps -alf | grep dludick" to see my list of running processes, I observe the following output:
Quote:
dludick@abacus:~> ps -alf | grep dludick
0 S dludick 22421 21141 0 77 0 - 948 wait 09:46 pts/1 00:00:00 /bin/sh ./parent
0 S dludick 22422 22421 0 77 0 - 686 - 09:46 pts/1 00:00:00 sleep 600
0 S dludick 22423 22421 0 77 0 - 686 - 09:46 pts/1 00:00:00 sleep 600
0 R dludick 22424 21177 0 77 0 - 618 - 09:46 pts/3 00:00:00 ps -alf
0 S dludick 22425 21177 0 75 0 - 713 pipe_w 09:46 pts/3 00:00:00 grep dludick
dludick@abacus:~>
The parent process and the 2 sleep (children) are clearly running.

When I then execute a ">kill -s SIGINT 22421" all the processes are killed correctly. I verify this by doing a "ps -alf | grep dludick" and observe:
Quote:
dludick@abacus:~> ps -alf | grep dludick
0 R dludick 22532 21177 0 77 0 - 617 - 10:00 pts/3 00:00:00 ps -alf
0 S dludick 22533 21177 0 75 0 - 714 pipe_w 10:00 pts/3 00:00:00 grep dludick
dludick@abacus:~>
Now, when I change the parent script to:
Code:
#!/bin/sh

#sleep 600 &
./child1 &
PID1="$!"

#sleep 600 &
./child2 &
PID2="$!"

trap "kill $PID1 $PID2" exit  INT
wait

where the child1 and child2 scripts are as follow

child1:
Code:
#!/bin/sh
echo starting proc 1, pid=$$
sleep 600

and child2:
Code:
#!/bin/sh
echo starting proc 2, pid=$$
sleep 600

(Note that each for each of the above scripts you also need to do ">chmod "+x" child1/2"

When again executing the parent via ">./parent" followed by ">ps -alf | grep dludick" I get the following active process summary:

Quote:
dludick@abacus:~> ps -alf | grep dludick
0 S dludick 22469 21141 0 77 0 - 948 wait 09:53 pts/1 00:00:00 /bin/sh ./parent
0 S dludick 22470 22469 0 79 0 - 1014 wait 09:53 pts/1 00:00:00 /bin/sh ./child1
0 S dludick 22471 22469 0 77 0 - 1014 wait 09:53 pts/1 00:00:00 /bin/sh ./child2
0 S dludick 22472 22470 0 79 0 - 685 - 09:53 pts/1 00:00:00 sleep 600
0 S dludick 22473 22471 0 77 0 - 685 - 09:53 pts/1 00:00:00 sleep 600
0 R dludick 22475 21177 0 77 0 - 618 - 09:53 pts/3 00:00:00 ps -alf
0 S dludick 22476 21177 0 75 0 - 714 pipe_w 09:53 pts/3 00:00:00 grep dludick
dludick@abacus:~>
Illustrating the correct "parent/child" relationships.

Now, when I kill the parent with a SIGINT ("> kill -s SIGINT 22469") I see that the following processes remain active:
Quote:
dludick@abacus:~> ps -alf | grep dludick
0 S dludick 22472 1 0 79 0 - 685 - 09:53 pts/1 00:00:00 sleep 600
0 S dludick 22473 1 0 77 0 - 685 - 09:53 pts/1 00:00:00 sleep 600
0 R dludick 22478 21177 0 77 0 - 617 - 09:54 pts/3 00:00:00 ps -alf
0 S dludick 22479 21177 0 75 0 - 714 pipe_w 09:54 pts/3 00:00:00 grep dludick
Does anybody know why the sleep processes are not being killed when each of the children receives a "SIGINT" signal? How can I modify the above scripts to ensure this?
# 5  
Old 10-15-2009
That's because the sleep procs didn't get a kill signal. The parent got it. It then killed the kids. But the sleeps (which are kids of the children of parent) did not get a kill. So they continue to sleep. Note that the parent of the sleeps have changed from the original parents to proc id 1 (aka init). The parents died, but the sleeps are still sleeping...
# 6  
Old 10-15-2009
Thank you for you quick reply, your advice worked.

I understand what you mean about the children of the children (i.e. the sleep processes) not getting the signal. I tried the following:

Changed "child1" to:
Code:
#!/bin/sh

echo starting proc 1, pid=$$
sleep 600 &
pid1=$!
trap "kill $pid1" exit INT
wait

and then "child2" to:
Code:
#!/bin/sh

echo starting proc 2, pid=$$
sleep 600 &
pid1=$!
trap "kill $pid1" exit INT
wait

so that the INT signals gets propagated to the spawned "sleep" children of each. When I now send a SIGINT to the original parent script, I see that the sleep processes are killed active. I think this solves the problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

parent and child directory

does anyone know how to check in an 'if' statement if a particular directory is a child directory of a particular directory? help ~ (2 Replies)
Discussion started by: ymc1g11
2 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Homework & Coursework Questions

Need help with deleting childīs parent and child subprocess

1. The problem statement, all variables and given/known data: I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the childīs process. 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: WhiteFace
0 Replies

4. UNIX for Advanced & Expert Users

Child Killing Parent

Hi all, I am writing a script which calls other third party scripts that perform numerous actions. I have no control over these scripts. My problem is, one of these scripts seems to execute and do what it is meant to do, but my calling / parent script always exits at that point. I need to... (4 Replies)
Discussion started by: mark007
4 Replies

5. Shell Programming and Scripting

multiple child scripts running in backgroud, how to use grep on the parent?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (5 Replies)
Discussion started by: albertashish
5 Replies

6. Shell Programming and Scripting

Error trapping in parent/child scripts

Greets all. I'm using Slackware 12.0 with the bash shell. Calling my scripts with /bin/sh... I'm building gnome-2.18.3 and I have all my build scripts ready and working but I'm calling them from a parent script which executes each child/build script in a certain order (for loop). I have "set... (6 Replies)
Discussion started by: madpenguin
6 Replies

7. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

8. UNIX for Dummies Questions & Answers

kill parent and child

Hello all, I have gone through the search and looked at posting about idle users and killing processes. Here is my question I would like to kill an idle user ( which I can do) but how can I asure that all of his process is also killed whit out tracing his inital start PID. I have tried this on a... (4 Replies)
Discussion started by: larry
4 Replies

9. Filesystems, Disks and Memory

How hard can it be? ps child/parent

:( Since I'm fairly new to the scene and don't have much experience in shell programming, I decided to check out the net for a useful script or two. What I'm looking for is a script that would let me enter a PID and then show the process tree associated with it. So it would display the (grand-)... (2 Replies)
Discussion started by: velde046
2 Replies

10. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies
Login or Register to Ask a Question