From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17399 invoked by alias); 16 Mar 2012 20:29:25 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 16897 Received: (qmail 7297 invoked from network); 16 Mar 2012 20:29:23 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: neutral (ns1.primenet.com.au: 74.125.82.171 is neither permitted nor denied by SPF record at ntlworld.com) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-proxyuser-ip:date:from:to:subject:message-id:in-reply-to :references:x-mailer:mime-version:content-type :content-transfer-encoding:x-gm-message-state; bh=emhesP7ns8T0XWWqK6QPR1YAxH2yK/xhJY2kUgt3JZo=; b=NgNT4E5FpBnNkeRVFLtXekfzSCKX4C5KOR0ABcgnaBxaMz7XrQmoWzQRn0FcVYHPth g8G5flXh7dcKqOHOk/e4flQ4jQt7/Faybcj+nx1drYCx+CUldZcbWIkY6qjHf/cJ3BM2 CLHdfveglnKL5Y3a2R79ftDiWEr5ls5VxBb3pOiNRgPjpyW8hdXXjAa9qiOHBqGt0ZcO bbVxWwqc9zqWsDU9I3ZQDeMBFRjZZLC4YEaERrTpNYIsyPfHVkAr263zBNDtQAjZ5yiI qTwq2gm2OpAb5TvEPbqq4nJMKCRdcjPacbY8LlUG+OmhBvG2Uu83uGTAM3vZJO0zMYrU Eu1Q== X-ProxyUser-IP: 86.6.29.42 Date: Fri, 16 Mar 2012 20:29:14 +0000 From: Peter Stephenson To: zsh-users@zsh.org Subject: Re: job control in a script / zsh parallelism problem. Message-ID: <20120316202914.1ba0b03a@pws-pc.ntlworld.com> In-Reply-To: References: X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQm9r+gkmgkTzhs+04xYYWV92ORG6see8cby1VqOT8YKnaqAzshJ0ajt+wfFB1x1YeUVigX1 On Thu, 15 Mar 2012 12:25:18 +0100 Simon Mages wrote: > In the following Script i tried to start 3 background processes, because > thats the only way i know to run things parallel in zsh, and write a "dot" > every two seconds during the runtime of this background Processes. > > #!/usr/bin/zsh > > #set -x > > for i in a b c > do > sleep 120 & > done > > jobs -l > > jobs -l |wc -l > > var=`jobs -l|wc -l` > > while [ $var -gt "0" ] > do > print "." > sleep 2 > var=`jobs -l|wc -l` > done > > The Problem is that "jobs -l" in the Script writes an complete background > process list to stdout but "jobs -l|wc -l" just prints a "0"? When i run > "jobs -l|wc -l" manual with some background jobs it is working fine. First answer: what's going on here. (I think you'll like the second answer better, however.) Normally, you don't get full job control in a non-interactive shell, only the basic features. When you run "jobs" at the start of a pipeline, it's in a subshell; there aren't any running processes in that subshell, so the shell reports nothing. In interactive shells, the shell option MONITOR is set, which gives you full job control. In this case, the shell does a little extra for you: it saves the job table when you enter a subshell and uses it if there are no jobs in the subshell. If you have a recent version of the shell you can set the MONITOR option in a non-interactive shell. You can do that just by putting "-m" after the "#!/usr/bin/zsh". If the shell's too old, you'll get an error when it tries to turn on job control. Note that, because this uses full job control, you get more verbose output than from starting background jobs in a normal script. Behind this is the fact that the "jobs" command was really originally designed for giving information to the user in interactive shells. Second answer: a better way. It's actually possible to use special parameters to examine the job states, and although this is specific to zsh it's much neater. See the zshmodules manual page and look for the zsh/parameter module, which has various parameters whose names begin with "job". Here's a slightly truncated version of your script testing the length of the $jobstates array that worked fine for me. #!/usr/bin/zsh zmodload zsh/parameter for i in a b c do sleep 120 & done while (( ${#jobstates} )) do print "." sleep 2 done -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/