kill child of shellscript

kill child of shellscript

Post by Vic Mortelman » Thu, 10 Jun 1999 04:00:00



suppose I have the following shellscript:

#!/bin/bash
command1 &
while sleep 30s; do
command2;
done

Command1 keeps running in the background, and the while-loop keeps
repeating itself every 30 seconds. That's what it's supposed to do
anyway.

But now, when I kill the script, command1 keeps running. How can I make
sure all commands invoked in the script are ended together with the
script ? Preferably in such a way that I don't need to use a special
form of the kill-command.

Greetings,

Vic

 
 
 

kill child of shellscript

Post by Ed Youn » Thu, 10 Jun 1999 04:00:00



> suppose I have the following shellscript:

> #!/bin/bash
> command1 &
> while sleep 30s; do
> command2;
> done

> Command1 keeps running in the background, and the while-loop keeps
> repeating itself every 30 seconds. That's what it's supposed to do
> anyway.

> But now, when I kill the script, command1 keeps running. How can I make
> sure all commands invoked in the script are ended together with the
> script ? Preferably in such a way that I don't need to use a special
> form of the kill-command.

Try killing the xterm you launched the shellscript from...

 
 
 

kill child of shellscript

Post by Scott Lanni » Thu, 10 Jun 1999 04:00:00


: > But now, when I kill the script, command1 keeps running. How can I
: > make sure all commands invoked in the script are ended together with
: > the script ? Preferably in such a way that I don't need to use a
: > special form of the kill-command.
:
: Try killing the xterm you launched the shellscript from...

Or he could also cycle the power....

Another solution might be to use 'trap' to trap the signal
you killed the script with. Trap allows to execute a command
when the trapped signal is received. 'man bash' (then
type "/trap" and bounce on 'n' (for next) several times to
get to the SHELL BUILTIN section)

--

"I do believe God gave me a spark of genius, but he quenched it
in misery." --Edgar Allan Poe

 
 
 

kill child of shellscript

Post by Niel Markwic » Thu, 10 Jun 1999 04:00:00




> > suppose I have the following shellscript:

> > #!/bin/bash
> > command1 &
> > while sleep 30s; do
> > command2;
> > done

> > But now, when I kill the script, command1 keeps running. How can I make
> > sure all commands invoked in the script are ended together with the
> > script ? Preferably in such a way that I don't need to use a special
> > form of the kill-command.

> Try killing the xterm you launched the shellscript from...

or trap the signal and kill the background process(es):

#!/bin/bash
command1 &
command1_pid=$!
trap "kill $command1_pid ; exit" INT HUP TERM

while sleep 30s; do
command2;
done

HTH,

Niel

--
Niel Markwick
Kontich, Belgium.

 
 
 

kill child of shellscript

Post by Scott Lanni » Thu, 10 Jun 1999 04:00:00


: Another solution might be to use 'trap' to trap the signal

Oh duh, I think you could just kill the job.. Killing the
"job" should kill all its child processes. Is that what you
meant?

(suspend with CTRL-z)
[1]  - Suspended           tin -q
[2]  + Suspended           testes

# kill %2
[2]    Terminated          testes
#

I used a similar script to what you wrote, and it worked.

--

"Only two things are infinite, the universe and human stupidity, and
I'm not sure about the former." --Albert Einstein

 
 
 

kill child of shellscript

Post by Vic Mortelman » Thu, 10 Jun 1999 04:00:00


I've implemented the trap-part and now it works fine: the background job
is killed when the script is killed.

But I've noticed that without the trap-part in it, there's a difference
using 'kill PID' or 'killall script' to kill the script. The second way
keeps the background job from the script alive (which was my problem).
When I say kill PID, the background job is killed as well.

But that was not possible for me, because it's xscreensaver that kills
the job for me, and it seems to use the 'killall' way of killing.

The actual script I use now is:

#!/bin/bash
urlget -f -s -m 120 $1 | jpegtran -optimize  > ~/.root.jpg
xv -root -rmode 5 -poll -smooth -max -viewonly ~/.root.jpg &
pid=$!
trap "kill $pid ; exit" INT HUP TERM
while sleep 60s; do
urlget -f -s -m 120 $1 | jpegtran -optimize  > ~/.root.jpg;
done

Setting this as the xscreensaver program to write to the root-window,
and giving the script as argument an URL of a vidcam-picture, the result
is quite nice. But without the trap in the script, every time the
screensaver started, an instance of xv stayed alive, which was no good
for my system performance...

Tanks for helping me out!

Greetings,

Vic


> : Another solution might be to use 'trap' to trap the signal

> Oh duh, I think you could just kill the job.. Killing the
> "job" should kill all its child processes. Is that what you
> meant?

> (suspend with CTRL-z)
> [1]  - Suspended           tin -q
> [2]  + Suspended           testes

> # kill %2
> [2]    Terminated          testes
> #

> I used a similar script to what you wrote, and it worked.

> --

> "Only two things are infinite, the universe and human stupidity, and
> I'm not sure about the former." --Albert Einstein