Gentoo Archives: gentoo-amd64

From: Duncan <1i5t5.duncan@×××.net>
To: gentoo-amd64@l.g.o
Subject: [gentoo-amd64] Re: How to use &&
Date: Sun, 04 Jan 2009 23:31:19
Message-Id: pan.2009.01.04.23.31.06@cox.net
In Reply to: Re: [gentoo-amd64] How to use && by Brett Johnson
1 Brett Johnson <brett@××××.com> posted 1231082331.30177.24.camel@homer,
2 excerpted below, on Sun, 04 Jan 2009 09:18:51 -0600:
3
4 > I have never looked into multi threading a shell script, though I
5 > suppose it could be possible but fairly complex. You could execute
6 > multiple jobs in the background using '&' and capture each process id
7 > into an array with the ${!} parameter, which "Expands to the process ID
8 > of the most recently executed background (asynchronous) command". Once
9 > you have launched the multiple jobs, you could enter a loop that sleeps
10 > a few seconds and then checks the status of each process id in the
11 > array, waiting for them to disappear.
12
13 I haven't done anything that advanced, but I do use a relatively simple
14 trick I learned from Mandrake's rc.sysinit (or whatever they call it, IDR
15 for sure, see my immediately previous post for the story) to allow
16 multiple background processes to execute in parallel, with a wait
17 barrier. Once you've seen it it's simple enough that perhaps you
18 discounted that as proper multi-threading... or perhaps you've just not
19 seen it done this way yet.
20
21 Here's how I did it in my esync script, which runs a esearch database
22 update and auto-fetches both @system and @world in parallel. There's
23 also a parallel sync function I used to sync layman's overlays at the
24 same time as portage, but I have the layman bit commented out now as I'm
25 not using any overlays besides my own personal one ATM. It's also worth
26 noting the trick I used to get the eupdatedb to run at the same niceness
27 portage uses from make.conf... and now that I look I see a nice example
28 of using && in place of an if/then, as well. =:^)
29
30 The one problem, of course, is that handling it this way means jumbled up
31 output. There are ways around that, but this was a quick-hack enough
32 that I didn't worry about doing the whole logging thing, etc, here.
33 "It works for me!" (TM) The wait builtin simply waits until all
34 background processes are completed before proceeding...
35
36 #!/bin/bash
37
38 sync-up () {
39 emerge --sync $@ &
40 # layman -S &
41 wait
42 }
43
44 finish-up () {
45 # emerge --regen
46 emerge -uDf @world &
47 emerge -uDf @system &
48
49 # we need PORTAGE_NICENESSS from make.conf to run eupdatedb
50 portnice=$(source /etc/make.conf;echo $PORTAGE_NICENESS)
51 [ -z $portnice ] && portnice=0
52 nice -n${portnice} eupdatedb &
53
54 wait
55 }
56
57 sync-up
58 finish-up
59 # end of script
60
61 --
62 Duncan - List replies preferred. No HTML msgs.
63 "Every nonfree program has a lord, a master --
64 and if you use the program, he is your master." Richard Stallman

Replies

Subject Author
[gentoo-amd64] Re: How to use && ABCD <en.ABCD@×××××.com>