Solved

Why do script children refuse to die?

Posted on 2004-03-30
5
642 Views
Last Modified: 2013-12-26
First of all, this is a Solaris question. I have a (bash) shell script abc.sh, which looks as follows:

#!/bin/bash
/some/perl/script.pl

The perl script is designed to hang (not by me), by trying to suck some data out from a bad URL (the URL never gives any data back) and there is no timeout.

Now, all I want to do is to be able to kill abc.sh with a single kill -9 without any residual processes hanging about afterwards. The problem is that the perl script refuses to die upon a kill -9 issued to the process of abc.sh.

Before issuing a SIGKILL (kill -9) to the process of abc.sh, a ps -ef shows the parent of the hung perl process to be abc.sh. After issuing a SIGKILL, the parent becomes 1 (/etc/init, as far as I remember). A subsequent kill -9 to the perl process kills it without any problems.

Can anyone think why the perl script would ignore the SIGKILL, which, AFAIK, gets passed through to it? If I am wrong in thinking that it gets passed to it, which is quite likely, then is there a way to pass it through nicely?

My main problem is that killing the script abc.sh is the only thing I can do (I have to use Java's Process.destroy() on it), as normally, I would not have the pid of any of its children (again, as the result of using Java).

Thanks.
0
Comment
Question by:tikiliainen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10712361
Hi tikiliainen,

The perl script is a separate process that is launched by your shell script ... Your signal is received by the shell srcipt and it exits ... However perl script is an independent process which has not received any signal ... thus it continues

kill sends the signal to a process not a process and all its children

you are right in remembering that init is the new parent (1)

Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10712377
one way around this is to put your group of processes that you wish to have killed in a separate process group and send the signal to -n where n is the process group number

man kill for more information on this
0
 
LVL 9

Expert Comment

by:Alf666
ID: 10712475
You simply want to send the shell a SIGHUP and not a SIGKILL. SIGHUP will instruct the shell to exit, but it will pass the signal to all child processes before.

Try and avoid using SIGKILL as it definitely kills processes without giving them a chance to clean up. SIGKILL is non catcheable.

kill -HUP <processid>

The only problem you will encounter is if your perl script blocks SIGHUP. Then, you'll have the same problem.

In this case, you can do the following :

#!/bin/bash
mySignalHandler() {
  kill -9 $MYPROC
  echo "done" > /tmp/fic
  exit
}

trap mySignalHandler SIGHUP

/some/perl/script.pl &

MYPROC=$!

wait ${MYPROC}


Of course, killing -9 the perl script is not the best option either, but if it resists to the standard HUP signal....
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10712882
Hi Alf666,
> kill -HUP <processid>

There is no "hang up" (i.e., losing connection) happening, so -TERM might be more appropriate, especially when the script is started with nohup (or via cron).

Cheers,
Stefan
0
 
LVL 9

Accepted Solution

by:
Alf666 earned 250 total points
ID: 10713565
NO NO NO !!!

The TERM signal is not propagated to the shell's children. No problem will be resolved for the question's author with SIGTERM.

The script should not be started with nohup, of course.

A small modification can be made to my script to have it handle SIGTERM though (one obvious reason for that is that kill sends SIGTERM by default) :

#!/bin/bash
mySignalHandler() {
  kill -TERM $MYPROC
  echo "done" > /tmp/fic
  exit
}

trap mySignalHandler SIGHUP SIGTERM

/some/perl/script.pl &

MYPROC=$!

wait ${MYPROC}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 40
if loop in java 3 174
canBalance challenge 34 110
Line meaning 9 96
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question