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

Re: Waiting for a process without using pid



On Sep 18, 11:51pm, Anonymous bin Ich wrote:
} Subject: Re: Waiting for a process without using pid
}
} On 09/16/2010 07:56 PM, Bart Schaefer wrote:
} > 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?
} >
} But I see that job2 is still running at the end, just the script having 
} this code forks at then kills both its instances.

If I understand that correctly, what you want is for the control script
AND both of its children to exit all at the same time, whenever at least
one of the children has exited?

That wasn't clear before.

Also after a bit of thought it's really not necessary to kill the
coprocess, you can do all the synchronization with I/O; and it's a
better solution to start the coprocess and set up the trap first,
rather than starting the background jobs first.

cat <<-\ENDOFA >a.sh
	#!/usr/bin/zsh

	coproc read -E
	trap "print Waking coprocess; print -p Woken" CHLD

	setopt HUP
	./b.sh 10 &
	echo $!
	./b.sh 20 &
	echo $!

	read -p -E
	print "Finished"
	kill -HUP -$$

	# Never get here
	exit 0
ENDOFA
cat <<-\ENDOFB >b.sh
	#!/usr/bin/zsh
	trap "print HUP $1 in $$; exit 0" HUP
	print start $1 in $$
	sleep $1
	print stop $1 in $$
ENDOFB

The trap in b.sh is just so you can see what's going on.

If the parent doesn't need to do anything after the first child exits,
you can avoid the coprocess entirely by having both children kill the
parent:

cat <<-\ENDOFA >a.sh
	#!/bin/zsh

	setopt HUP

	repeat 2 {
		: $RANDOM to prime the number generator
		{ ./b.sh $((RANDOM%10+10)); kill -HUP -$$ } &
	}

	wait
ENDOFA



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