Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Jan 2013 14:30:09 -0500
From:      Greg Larkin <glarkin@FreeBSD.org>
To:        Nikos Vassiliadis <nvass@gmx.com>
Cc:        Free BSD Questions list <freebsd-questions@freebsd.org>
Subject:   Re: assigning values to variables in the background
Message-ID:  <50F5AE41.1040305@FreeBSD.org>
In-Reply-To: <50F594E9.7000606@gmx.com>
References:  <50F594E9.7000606@gmx.com>

next in thread | previous in thread | raw e-mail | index | archive | help
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 1/15/13 12:42 PM, Nikos Vassiliadis wrote:
> Hi,
> 
> A bit of an OT question. I am writing a bourne shell script that 
> collects data from a router. Since netstat & vmstat can run for a
> numbers of iterations I thought I would use just that:
> 
> stats() ( nstats=`netstat -I ng0 -q 1 60 | tail -1` & 
> rawdata=`vmstat -c 2 60 | tail -1` wait ...
> 
> The logic was: 1. run the first process in the bg 2. run the second
> process 3. wait to make sure the first process has finished 4.
> continue further
> 
> It makes perfect sense why this doesn't work. Both commands run in
> the foreground.
> 
> I am going to split the time between netstat and vmstat. So, it 
> will be 30 seconds of netstat and 30 seconds of vmstat.
> 
> But I am still interested/curious how one should go for this using 
> the shell. So, can this be done without files? Any thoughts?
> 
> Thanks, Nikos


Hi Nikos,

As far as I can tell, the backticks are what's causing the problem.
Even though you put the first command in the background (maybe with
the & inside the backticks, though), the assignment to the nstats
variables causes the script to block.

If you switch to using temp files, you may have more luck, e.g.:

netstat -w 1 -I ng0 -q 60 | tail -1 > /tmp/netstat.$$ &
npid=`echo $!`
vmstat -w 2 -c 60 | tail -1 > /tmp/vmstat.$$ &
vpid=`echo $!`
wait $npid
nstats=`cat /tmp/netstat.$$`
rm -f /tmp/netstat.$$
wait $vpid
rawdata=`cat /tmp/vmstat.$$`
rm -f /tmp/vmstat.$$`

Hope that helps,
Greg
- -- 
Greg Larkin

http://www.FreeBSD.org/           - The Power To Serve
http://www.sourcehosting.net/     - Ready. Set. Code.
http://twitter.com/cpucycle/      - Follow you, follow me
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.13 (Darwin)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlD1rkAACgkQ0sRouByUApCQogCgwOWapKTe9Wl+EClhHZ8iHtn+
/hUAniKZZq1Se2DEtTe1+OAsxDw0f++Z
=zCkZ
-----END PGP SIGNATURE-----



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?50F5AE41.1040305>