<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Readr: Updates from jzawodn</title>
    <link>http://readr.com/profile/jzawodn</link>
    <language>en-us</language>
    <item type="generic">
      <title>Bash Trick: Watching Multiple Background Jobs</title>
      <description>&lt;p&gt;I recently had a need to add some error checking to a &lt;a href="http://www.gnu.org/software/bash/"&gt;bash&lt;/a&gt; script that runs multiple copies of a Perl script in parallel to better utilize a multi-core server.  I wanted a way to run these four processes in the background and gather up their exit values.  Then, if any of them failed, I'd prematurely exit the bash script and report the error.&lt;/p&gt;

&lt;p&gt;After a bit of reading bash docs, I came across some built-ins that I hadn't previously used or even seen.  First, I'll show you the code:&lt;/p&gt;

&lt;h3&gt;wait.sh&lt;/h3&gt;

&lt;p&gt;&lt;a hef="http://gist.github.com/raw/27452"&gt;This is the bash script&lt;/a&gt; that runs the parallel processes and gathers up the exit values.&lt;/p&gt;

&lt;script src="http://gist.github.com/27452.js"&gt;&lt;/script&gt;
&lt;noscript&gt;
#!/bin/bash

FAIL=0

echo "starting"

./sleeper 2 0 &amp;
./sleeper 2 1 &amp;
./sleeper 3 0 &amp;
./sleeper 2 0 &amp;

for job in `jobs -p`
do
    echo $job
    wait $job || let "FAIL+=1"
done

echo $FAIL

if [ "$FAIL" == "0" ];
then
    echo "YAY!"
else
    echo "FAIL! ($FAIL)"
fi
&lt;/noscript&gt;

&lt;h3&gt;sleeper&lt;/h3&gt;

&lt;p&gt;And &lt;a href="http://gist.github.com/27454"&gt;here's the Perl script&lt;/a&gt; that I wrote in order to test the functioning of &lt;tt&gt;wait.sh&lt;/tt&gt;.  It accepts to arguments.  The first is the number of seconds to sleep (to simulate the delay associated with doing work) and the second is the exit value it should use (any non-zero value indicates a failure).&lt;/p&gt;

&lt;script src="http://gist.github.com/27454.js"&gt;&lt;/script&gt;
&lt;noscript&gt;
#!/usr/bin/perl -w

use strict;

my $time = $ARGV[0] || 1;
my $exit = $ARGV[1] || 0;

sleep $time;
exit  $exit;
&lt;/noscript&gt;

&lt;h3&gt;Discussion&lt;/h3&gt;

&lt;p&gt;New to me was the use of &lt;em&gt;let&lt;/em&gt; to do math on a variable so that I can count up the number of failures.  Is there a better way?  There's no native ++ operator in bash.  Similarly, using &lt;em&gt;jobs&lt;/em&gt; to get a list of pids to &lt;em&gt;wait&lt;/em&gt; on provided to be a very useful idiom.&lt;/p&gt;

&lt;p&gt;The code is straightforward and works for my purposes.  But since 99% of my time is spent in Perl rather than bash, I wonder what I could have done differently and/or better.  Feedback welcome.&lt;/p&gt;

&lt;p&gt;And, if this is at all useful to you, feel free to take it and run...&lt;/p&gt;

&lt;p&gt;Finally, I'm starting to really dig &lt;a href="http://gist.github.com/"&gt;gist.github&lt;/a&gt; for showing off bits of code.  It's good stuff.&lt;/p&gt;  &lt;p&gt;(&lt;a href="http://jeremy.zawodny.com/blog/archives/010717.html#comments"&gt;comments&lt;/a&gt;)&lt;/p&gt;</description>
      <author>jzawodn</author>
      <link>http://feeds.zawodny.com/~r/jzawodn/rss2/~3/460860986/010717.html</link>
      <pubDate>Fri, 21 Nov 2008 10:03:26 -0600</pubDate>
    </item>
    <item type="generic">
      <title>TV Watching and Happiness</title>
      <description>&lt;p&gt;In one of those "well, duh!" moments the other day, I came across a headline on Slashdot that said &lt;a href="http://science.slashdot.org/article.pl?sid=08/11/15/192222"&gt;Unhappy People Watch More TV&lt;/a&gt;.  Given that I mostly &lt;a href="http://jeremy.zawodny.com/blog/archives/001486.html"&gt;stopped watching TV&lt;/a&gt; quite some time ago and consider it to be one of the more &lt;a href="http://jeremy.zawodny.com/blog/archives/002434.html"&gt;rude devices in our culture&lt;/a&gt;, I clicked thru to read about how others have discovered what I'd already guessed was true...&lt;/p&gt;

&lt;blockquote&gt;A new study by sociologists at the University of Maryland concludes that unhappy people watch more TV, while people who describe themselves as 'very happy' spend more time reading and socializing. 'TV doesn't really seem to satisfy people over the long haul the way that social involvement or reading a newspaper does,' says researcher John P. Robinson. 'It's more passive and may provide escape--especially when the news is as depressing as the economy itself.&lt;/blockquote&gt;

&lt;p&gt;Imagine that...  Stagnation and exposure to negative information leads to sadness.  It goes on...&lt;/p&gt;

&lt;blockquote&gt;The data suggest to us that the TV habit may offer short-run pleasure at the expense of long-term malaise.' Unhappy people also liked their TV more: 'What viewers seem to be saying is that while TV in general is a waste of time and not particularly enjoyable, "the shows I saw tonight were pretty good.&lt;/blockquote&gt;

&lt;p&gt;Another shock.  TV provides only a short-term reward (kind of like a drug hit).&lt;/p&gt;

&lt;p&gt;If this resonates with you a bit, or you suspect deep down that there's more going on with the influence of TV in our culture, I highly recommend reading &lt;a href="http://www.amazon.com/Amusing-Ourselves-Death-Discourse-Business/dp/014303653X/ref=nosim/jeremydzawodny"&gt;Amusing Ourselves To Death&lt;/a&gt; by Neil Postman if you have not already.&lt;/p&gt;

&lt;p&gt;It's too bad this stuff doesn't get caught in school--where, I'm told, teachers are using PowerPoint more and more.&lt;/p&gt;

&lt;p&gt;*sigh*&lt;/p&gt;  &lt;p&gt;(&lt;a href="http://jeremy.zawodny.com/blog/archives/010706.html#comments"&gt;comments&lt;/a&gt;)&lt;/p&gt;</description>
      <author>jzawodn</author>
      <link>http://feeds.zawodny.com/~r/jzawodn/rss2/~3/455841280/010706.html</link>
      <pubDate>Mon, 17 Nov 2008 05:36:06 -0600</pubDate>
    </item>
  </channel>
</rss>
