--killing backround Procs spawned from the parent script with Ctrl+C trap


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting --killing backround Procs spawned from the parent script with Ctrl+C trap
# 1  
Old 10-25-2019
--killing backround Procs spawned from the parent script with Ctrl+C trap

Hello:

Am trying to understand why the method #2 works but method #1 does not.
For both methods, sending CTRL+C should kill both the Parent script & all of the spanwd background procs.

Method #1:
==========================
Code:
#!/bin/sh

ctrl_c()
{
        echo "** Trapped CTRL-C"
        sudo kill -9 $TEST_PARALLEL_1 $TEST_PARALLEL_2; exit
}

trap "ctrl_c" INT

./TEST-PARALLEL-1.sh &
TEST_PARALLEL_1=$!

./TEST-PARALLEL-2.sh
TEST_PARALLEL_2=$!

echo $TEST_PARALLEL_1 $TEST_PARALLEL_2 )
/usr/bin/wait $TEST_PARALLEL_1 $TEST_PARALLEL_2

[aa229082@esmqst13:/home/aa229082 ] ps -ef | grep TEST
aa229082 123053      1  0 13:19 pts/1    00:00:00 /bin/sh ./TEST-PARALLEL-1.sh
aa229082 123054      1  0 13:19 pts/1    00:00:00 /bin/sh ./TEST-PARALLEL-2.sh

Method #2:
=========================
Code:
(trap 'kill 0' SIGINT

./TEST-PARALLEL-1.sh &
TEST_PARALLEL_1=$!

./TEST-PARALLEL-2.sh
TEST_PARALLEL_2=$!

echo $TEST_PARALLEL_1 $TEST_PARALLEL_2 )

[aa229082@esmqst13:/home/aa229082 ] ps -ef | grep TEST
aa229082   1439 120859  0 14:52 pts/1    00:00:00 /bin/sh ./TEST-PARALLEL.sh
aa229082   1440   1439  0 14:52 pts/1    00:00:00 /bin/sh ./TEST-PARALLEL.sh
aa229082   1441   1440  0 14:52 pts/1    00:00:00 /bin/sh ./TEST-PARALLEL-1.sh
aa229082   1442   1440  0 14:52 pts/1    00:00:00 /bin/sh ./TEST-PARALLEL-2.sh

So for method #1, the spawned BG Procs have init as the PPID.
so even the "sudo kill -9" does not kill them

for method #2, we are using "kill 0", but this works.
But "kill 0" does not actually kill but :

"If sig is 0, then no signal is sent, but error checking is still performed."

So looks like Method #2 works solely because the whole thing is executed
from the Sub-Shell.

Curiously, the additional commands like "echo $TEST_PARALLEL_1 $TEST_PARALLEL_2"
did not show on the STDOUT.

Just trying to better understand the whole process.

Thnx
# 2  
Old 10-25-2019
Please enlighten us with the exact operating system / distribution / version you are using?
# 3  
Old 10-28-2019
tested on both Solaris & Redhat:

SunOS esmqst09 5.11 11.4.5.3.0 sun4v sparc sun4v

Linux esmqst13 3.10.0-957.21.3.el7.x86_64 #1 SMP Fri Jun 14 02:54:29 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux
# 4  
Old 10-28-2019
What do you think does kill 0?
It sends a default kill (TERM) to pid 0.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trap killing PID

Hi, Just wonder if there is a way to identify the PID of the killing process using trap. Please let me know possible solution. #!/bin/ksh hello () { print "in hello"; print "PID of process issued SIGNAL"; --> this is what i'm looking for. } trap hello SIGKILL SIGTERM while... (3 Replies)
Discussion started by: Gajendra_PH
3 Replies

2. Shell Programming and Scripting

Help with getting a Ctrl-C trap working w/ a piped tail -f...

Hi All, Although each line below seems to work by itself, I've been having trouble getting the Control-C trap working when I add the "|perl -pe..." to the end of the tail -f line, below. (That |perl -pe statement basically just adds color to highlight the word "ERROR" while tailing a log... (2 Replies)
Discussion started by: chatguy
2 Replies

3. Shell Programming and Scripting

KSH to execute stored procs/ctrl files

I am needing to call sqlplus and sqlldr in the same korn shell script. Below are the steps that I need to accomplish. I am new to Oracle shell scripting so an example would be great. CBOB_LOAD.KSH - script name Calls Oracle stored procedure sp_CBOB_CLEAR_LN_STAGING (SERVER: TESTSVR100) Calls... (0 Replies)
Discussion started by: acewilli
0 Replies

4. Shell Programming and Scripting

Trap CTRL-C and background process

Hello, I have a script which copies via scp several large files to a remote server. What I want is that even if someone hits CTRL-C, the scp commands continues till the end. Here is what I wrote #! /bin/bash function testFunction { echo "COPY START" scp large.tar.gz... (11 Replies)
Discussion started by: RobertFord
11 Replies

5. Shell Programming and Scripting

trap CTRL-C problem

I am trying to trap CTRL-C, now the program I call has it's own exit message, I think this is the problem .. This is what I have now : function dothis { echo 'you hit control-c' exit } function settrap { trap dothis SIGINT } settrap until false; do ./ITGRecv.exe doneDoing this I... (2 Replies)
Discussion started by: Pmarcoen
2 Replies

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

7. UNIX for Advanced & Expert Users

trap ctrl c in shell script

how to trap the ctrl c in unix shell script my script is running in while loop it should not be terminate with ctrl c. if i press ctrl c while running script it shloud ignore the same. please healp.......... thanks in advance (2 Replies)
Discussion started by: arvindng
2 Replies

8. Shell Programming and Scripting

Killing parent shells from subshells (KSH)

Hi all, I have a shell script which calls other shell scripts, depending on the input. Within a.sh, I have a command which calls b.sh (ie. ksh b.sh) Normally, we use the exit function to terminate a shell. However, if I choose to call exit from b.sh, I will return to the parent shell who... (4 Replies)
Discussion started by: rockysfr
4 Replies

9. UNIX for Dummies Questions & Answers

forking and killing parent processes

Hi everybody, I'm having some problems wiriting a program in UNIX using the "fork" and "kill" system calls. I have to create a C program P0, which creates 9 other processes P1, P2, ..., P9, where P0 is the father of P1, P1 the father of P2, and so on. All the processes contain an infinite... (0 Replies)
Discussion started by: davewilliams20
0 Replies

10. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies
Login or Register to Ask a Question