[NTLUG:Discuss] script hang

Kevin Brannen kbrannen at pwhome.com
Thu Mar 10 20:21:27 CST 2005


Kyle Davenport wrote:

>*** Authentication Certificate ***
>
>Kevin Brannen wrote:
>  
>
>>Um, why do you have the eval in there?  Does the ddr1394 command really
>>return something you need to pull back into your current (loop)
>>process?  My guess would be no, so remove the eval and the single quotes.
>>    
>>
>
>That is the only way to background a process from within a loop.
>  
>

Nope, sorry, unless there is something else you're not telling us. (a 
little bit of CYA :-)

>I might have written something in expect, but usually I have more problems
>with blocking pipes that way.
>  
>

I understand the power of "expect", but don't care for it.

>I played with it some more last night.  If the ddr1394 works the first
>time, the script runs fine.  If it starts looping - killing the previous
>ddr1394 and retrying, ddr1394 fails after the 3rd retry with a segmentation
>fault!  I think I'm missing something about bash job control...
>  
>

Here an example, consider "one" -- a bash script:

#!/bin/bash
while true
do
     two >out 2>err &
     pid=$!
     sleep 3
     if grep errorstring err
     then
           echo killing $pid
           kill $pid
     else
           break
     fi
done
echo "loop done"
exit(0)

I've made a few changes, but they should be mainly stylistic.  Now for 
the child process "two":

#!/usr/bin/perl
if (rand(100) > 10)
{
    print STDERR "errorstring\n";
}
else
{
    print "small number\n";
}
sleep(10);  # live until we're killed

I resorted to Perl for "two" because I couldn't remember how to force 
something to STDERR in bash and was too lazy to research it. :-)  Now 
the output:

[75 ~ /home/kevin/tmp5] one
errorstring
killing 14581
./one: line 14: 14581 Terminated              two >out 2>err
errorstring
killing 14584
./one: line 14: 14584 Terminated              two >out 2>err
...[7 more of these things]...
killing 14608
./one: line 14: 14608 Terminated              two >out 2>err
loop done
[76 ~ /home/kevin/tmp5]

Change the grep to "grep -q" to remove the "errorstring" output.  
Nevertheless, you can see that the subprocess does go into the 
background successfully, and we kill it successfully, and eventually the 
random number generator "hits" and allows us to stop.  Try taking this 
example and building on it.

Also, you may have a problem with ddr1394 not behaving correctly.  It 
may be nothing in your script. :-)  But I will also admit, your "if" 
statement looks like psuedo code to me, not real script.  That may be 
legal in bash, but it's nothing I've seen before.  I'll also admit I do 
most of my script work in ksh not bash, so you might want to ignore that 
observation. ;-)

HTH,
Kevin




More information about the Discuss mailing list