Operating System - HP-UX
1833875 Members
2021 Online
110063 Solutions
New Discussion

Re: ^C signal trap problem.

 
SOLVED
Go to solution
Rajeev Tyagi
Valued Contributor

^C signal trap problem.

Hi,

My requiremnet is when i press ^c during Menu based child script execution i do not want parent to get killed it is only child which should get killed and i should be able to return back to main menu. I have used following code to acheive this purpous.

Parent Script
---------------
#!/bin/ksh
trap "" 2

Child script called from this menu


child script
-----------------
#!/bin/ksh
trap 2 #untrap ^c signal
tail -f "Some file name"


But i am not able to acheive this i can not come out of tail -f command by pressing ^c key. I will appreciate if somebody can correct me.

Regards
Rajeev
6 REPLIES 6
Jeff Machols
Esteemed Contributor

Re: ^C signal trap problem.

you can run trap 2 right before you call the child script

#!/bin/ksh
trap "" 2
code
...
code

trap 2
child_script.sh
trap "" 2

Craig Rants
Honored Contributor
Solution

Re: ^C signal trap problem.

Have you tried executing the child script independently of the parent script, then trying your Cntl-C? Your trap syntax looks ok so this test should tell you if the trap command from the parent or child script is being used.

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
John Palmer
Honored Contributor

Re: ^C signal trap problem.

Hi,

Jeff is correct. Excerpt from man sh-posix:-

Any attempt to set a trap on a signal that was ignored upon entering the current shell is ineffective.

Your parent shell ignores signal 2 so the child will too!

Regards,
John
Jeff Machols
Esteemed Contributor

Re: ^C signal trap problem.

ans the problem is you still gave to "go through" the parent shell. so even if you did something like this

trap "" 2
. child.sh


####### child.sh #######
trap 2
tail file


the trap will be gone, but the interrupt will hit the parent first
Rajeev Tyagi
Valued Contributor

Re: ^C signal trap problem.

all,

thanks for your valuable replies. My problem got solved i did something like this in child.

trap "kill -9 `ps -ef | grep $$| grep -v grep | awk '$3=='$$'{print $2}'" 2

and it worked.

Once again thanks to you all and wish you all a very happy new year.

Rajeev
John Palmer
Honored Contributor

Re: ^C signal trap problem.

One other option which doesn't involve kill -9:-

In the parent script, ignore signal 2 by executing a dummy function:

#!/usr/bin/sh
function ignoreit {
return
}

trap "ignoreit" 2

Call child etc

Child script
===========

#!/usr/bin/sh
trap 2
tail -f ...

Regards,
John