mbert
mbert

Reputation: 1515

Shell script kill background process on quit

I have a script like this:

#!/bin/sh
exec ./cmd1&
exec ./cmd2

If I kill the script only cmd2 is killed, cmd1 keeps running. Is it possible that both processes quit automatically?

Upvotes: 2

Views: 1042

Answers (1)

yrk
yrk

Reputation: 164

do not do second exec (the first one is redundant, too) but leave the shell wait for it. killing the shell may kill the commands; if not - then:

    trap 'kill -15 $kids; exit 143' TERM
    cmd1 &
    kids=$!
    cmd2 &
    kids="$kids $!"
    wait

Upvotes: 3

Related Questions