[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Why do script children refuse to die?

Posted on 2004-03-30
5
Medium Priority
?
663 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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

656 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