Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Waiting for a process without using pid



On Sep 16,  4:09pm, Anonymous bin ich wrote:
}
} I am trying to write a 'timeout' script, which will take 2 commands
} and exit after whichever one exits first. Is there a way to do it
} without using pid or polling?

Sure.

    job1 &
    job2 &
    coproc read
    trap "trap - CHLD ; kill $! " CHLD  # Here $! is PID of coprocess
    read -p

This sets up a coprocess child that's blocking on the parent, and then
makes the parent block on the child, creating a deadlock.  The trap
breaks the deadlock by killing the coprocess, at which point the
parent wakes up.

You might need to prefix job1 and job2 with "sleep 2 ;" to prevent a
race condition where one of the jobs exits before the trap is ready.



Messages sorted by: Reverse Date, Date, Thread, Author