Solved

Why do script children refuse to die?

Posted on 2004-03-30
5
581 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 157
cat dog challenge 18 122
no14 challenge 14 57
wordcount challenge 11 78
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now