zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: job control in a script / zsh parallelism problem.
Date: Fri, 16 Mar 2012 20:29:14 +0000	[thread overview]
Message-ID: <20120316202914.1ba0b03a@pws-pc.ntlworld.com> (raw)
In-Reply-To: <CACxPiz3wYTeiyBggxTO7m_AOtav2psa_J2oXMMTU1Ax2CfxHJA@mail.gmail.com>

On Thu, 15 Mar 2012 12:25:18 +0100
Simon Mages <mages.simon@googlemail.com> 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 <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


      reply	other threads:[~2012-03-16 20:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-15 11:25 Simon Mages
2012-03-16 20:29 ` Peter Stephenson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120316202914.1ba0b03a@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).