From owner-freebsd-questions@FreeBSD.ORG Thu Nov 1 17:10:54 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 35A7A16A419 for ; Thu, 1 Nov 2007 17:10:54 +0000 (UTC) (envelope-from jan.grant@bristol.ac.uk) Received: from dirg.bris.ac.uk (dirg.bris.ac.uk [137.222.10.102]) by mx1.freebsd.org (Postfix) with ESMTP id EAC7213C48E for ; Thu, 1 Nov 2007 17:10:53 +0000 (UTC) (envelope-from jan.grant@bristol.ac.uk) Received: from mail.ilrt.bris.ac.uk ([137.222.16.62]) by dirg.bris.ac.uk with esmtp (Exim 4.67) (envelope-from ) id 1IndYS-0001c3-96; Thu, 01 Nov 2007 17:10:12 +0000 Received: from cse-jg.cse.bris.ac.uk ([137.222.12.37]:64746) by mail.ilrt.bris.ac.uk with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1IndY7-0005X7-Ts; Thu, 01 Nov 2007 17:09:43 +0000 Date: Thu, 1 Nov 2007 17:09:43 +0000 (GMT) From: Jan Grant X-X-Sender: cmjg@tribble.ilrt.bris.ac.uk To: David Naylor In-Reply-To: Message-ID: <20071101170331.A41569@tribble.ilrt.bris.ac.uk> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-ILRT-MailScanner: Found to be clean X-ILRT-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-2.701, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 1.70, BAYES_00 -2.60) X-ILRT-MailScanner-From: jan.grant@bristol.ac.uk X-Spam-Status: No X-Spam-Score: -0.8 X-Spam-Level: / Cc: freebsd-questions@freebsd.org Subject: Re: sh script difficulties (running parallel functions) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Nov 2007 17:10:54 -0000 On Thu, 1 Nov 2007, David Naylor wrote: > Hi, > > I am having a hard time getting (very complex script for me) to work. The > basic idea is that this script runs a bunch of tarkets, many of which are > time consuming but low on resources (such as downloading files). Now if I > run the tarkets all at once (given some dependancy issues) it greatly speeds > up the process (about 5 time speed increase). However I do not know how to > do this using sh... > > Example > > #!/bin/sh > > worker1() { > # Copy some files > } > > worker2() { > # Download some files > } > > worker3() { > # Do something else > } > > ..... # and so on > > run_jobs() { > worker1 & > worker2 & > worker3 & > # !!! Somehow wait for over workers to finish before continuing !!! > } > > #Finished > > Furthermore, how can signals be handled such that the signals get > accumulated and once all the other workers have finished the signals get > passed on (appropriately) The "wait" shell builtin is part of what you're after. You probably will need to trap the signals you're interested in catching. Just a trap 'int=1' INT wait trap - INT if [ "x$int" = x1 ]; then ... ; fi should do it. -- jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/ Tel +44 (0)117 3317661 http://ioctl.org/jan/ There's no convincing English-language argument that this sentence is true.