Sponsored Content
Top Forums Shell Programming and Scripting --killing backround Procs spawned from the parent script with Ctrl+C trap Post 303040261 by gilgamesh on Friday 25th of October 2019 04:25:02 PM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
TEST(1) 						      General Commands Manual							   TEST(1)

NAME
test - condition command SYNOPSIS
test expr DESCRIPTION
test evaluates the expression expr, and if its value is true then returns zero exit status; otherwise, a non zero exit status is returned. test returns a non zero exit if there are no arguments. The following primitives are used to construct expr. -r file true if the file exists and is readable. -w file true if the file exists and is writable. -f file true if the file exists and is not a directory. -d file true if the file exists and is a directory. -s file true if the file exists and has a size greater than zero. -t [ fildes ] true if the open file whose file descriptor number is fildes (1 by default) is associated with a terminal device. -z s1 true if the length of string s1 is zero. -n s1 true if the length of the string s1 is nonzero. s1 = s2 true if the strings s1 and s2 are equal. s1 != s2 true if the strings s1 and s2 are not equal. s1 true if s1 is not the null string. n1 -eq n2 true if the integers n1 and n2 are algebraically equal. Any of the comparisons -ne, -gt, -ge, -lt, or -le may be used in place of -eq. These primaries may be combined with the following operators: ! unary negation operator -a binary and operator -o binary or operator ( expr ) parentheses for grouping. -a has higher precedence than -o. Notice that all the operators and flags are separate arguments to test. Notice also that parentheses are meaningful to the Shell and must be escaped. SEE ALSO
sh(1), find(1) TEST(1)
All times are GMT -4. The time now is 03:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy