Search code examples
shelltimeoutwait

kill bash script with wait command


A bash script demo.sh

#!/bin/bash ./prog1 & ./prog2 & wait

Use timeout -s 9 5m demo.sh to run the script.

The script demo.sh used to be without & and wait. I want to know whether timeout will kill prog1 and prog2 when timeout happens. How can I make sure that all subprocesses would be killed?


Solution

  • The forked jobs will be killed when you kill the shell process started by demo.sh (unless you do something like disown $PID).

    You can ensure this happens with kill -0:

    ./prog1 &
    echo P1=$!
    ./prog2 &
    echo P2=$!
    

    you can then kill -0 ${PID1} and kill -0 ${PID2} and ensure that both commands return with exit status 1, which means "couldn't find process"