[TriLUG] Making a bg'd bash script wait for user intervention

Brian Henning brian at strutmasters.com
Mon Oct 18 16:12:54 EDT 2004


Unless I'm misunderstanding something, the problem with read being
backgrounded is that it gets detached from stdin (and I have no idea where
it then takes its input from).  Experimentation shows that a backgrounded
read causes the process to enter the "stopped" state.  If I'm operating in
the shell that invoked the command, such as by having previously typed
"waittest.sh &" (see example output below), I can type fg to bring the task
to the foreground and reattach read to stdin.  Unfortunately, fg only takes
job numbers (not process IDs) as arguments, meaning only the owner process
can then bg or fg the stopped task (for example, I can't stop it in one
shell and resume it in another).  Since this is ultimately intended to be
called from a cron task, that doesn't sound like behavior that will be
useful.

Example output:
[me at it] $ cat waittest.sh
#!/bin/bash
read foo ;
echo "You typed: $foo" ;
[me at it] $ ./waittest.sh &
[me at it] $ <enter> # just to get it to spit out the stopped message

[1] +  Stopped        waittest.sh
[me at it] $ fg
./waittest.sh
<enter>
You typed:
[me at it] $         # and now waittest.sh has exited

.. Well it looks like Jeremy has stepped in and already explained much of
what I just found out.  I was hoping there would be a nice clean way to do
it (such as what I had hoped to achieve with suspend)..  I suppose I could
create a named pipe and have read block on that..

Anyhow, thanks for the suggestions so far, and if anyone else thinks of
anything, by all means please pipe up. :-)

Thanks,
~Brian




----- Original Message ----- 
From: "Ryan Leathers" <Ryan.Leathers at globalknowledge.com>
To: "'Triangle Linux Users Group discussion list'" <trilug at trilug.org>
Sent: Monday, October 18, 2004 3:41 PM
Subject: RE: [TriLUG] Making a bg'd bash script wait for user intervention


> have you tried "read" ? ? don't know if backgrounding it will cause
troubles
> but I'll bet you could test it easily enough.  basically read will just
wait
> until you press enter.  the variable you set is of no consequence.
>
> -----Original Message-----
> From: Brian Henning [mailto:brian at strutmasters.com]
> Sent: Monday, October 18, 2004 3:10 PM
> To: TriLUG
> Subject: [TriLUG] Making a bg'd bash script wait for user intervention
>
>
> Hi Y'all,
>   I'm working on a set of scripts for backups, and I've hit a snag in a
> script that burns images to CDs.  I need it to wait for user intervention
in
> certain situations (no CD in drive, CD not blank, swap CDs now, etc).
I've
> got the various conditions mapped out in the script; all I need is a way
to
> make it wait for me to say "go", basically.  The trick is that the process
> may be backgrounded, so it can't be simply waiting for Enter on stdin (or
> can it?).  I tried suspend, but I get
> suspend: cannot suspend: no job control
>
> Is there another simple method to make a bash script wait for a signal?
Or
> else, what do I need to do to make suspend work?
>
> Thanks muchly,
> ~Brian
>
> ----------------
> Brian A. Henning
> Strutmasters.com
> 866.597.2397
> ----------------
>
>
> -- 
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ  : http://trilug.org/faq/
> TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
> TriLUG PGP Keyring         : http://trilug.org/~chrish/trilug.asc
> -- 
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ  : http://trilug.org/faq/
> TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
> TriLUG PGP Keyring         : http://trilug.org/~chrish/trilug.asc
>





More information about the TriLUG mailing list