unblocking a parent shell after it launches a subshell

unblocking a parent shell after it launches a subshell

Post by Karl Bell » Fri, 12 Apr 2002 02:10:29



I tried searching but I couldn't find an answer to this.

I have a csh script, lets call it A, that runs another csh script,
lets call it B.

I want script A to wait on script B until B has done a few things.
But, then I want script A to proceed leaving B to finish. How is this
possible?

If I run script B in the foreground, script A is blocked entirely. If
I run script B in the background, script doesn't wait around and
continues.

 
 
 

unblocking a parent shell after it launches a subshell

Post by Bill Marc » Fri, 12 Apr 2002 02:25:44


On 10 Apr 2002 10:10:29 -0700,


>I tried searching but I couldn't find an answer to this.

>I have a csh script, lets call it A, that runs another csh script,
>lets call it B.

>I want script A to wait on script B until B has done a few things.
>But, then I want script A to proceed leaving B to finish. How is this
>possible?

>If I run script B in the foreground, script A is blocked entirely. If
>I run script B in the background, script doesn't wait around and
>continues.

Can you arrange for script B to write a file after it has done "a few
things"?  Then you can do something like
script_B &
until [ -f done_it ]; do
  sleep 1
done
rm done_it

 
 
 

unblocking a parent shell after it launches a subshell

Post by David Thompso » Fri, 12 Apr 2002 02:26:00



Quote:> I tried searching but I couldn't find an answer to this.

> I have a csh script, lets call it A, that runs another csh script,
> lets call it B.

> I want script A to wait on script B until B has done a few things.
> But, then I want script A to proceed leaving B to finish. How is this
> possible?

You'll have to design this yourself.  Ie, have script B
touch a file when it reaches a certain point.  But first A
starts B in in the background, then A immediately goes into
a loop, testing if B has been created this file yet; A sleeps
some amount in between each test.

Once B creates the file, A will notice it and stop looping,
A should then delete this file and continue on.

Obviously changes are needed to A and B; make sure they use
the same filename!

--
David Thompson

 
 
 

unblocking a parent shell after it launches a subshell

Post by Kevin Rodger » Fri, 12 Apr 2002 03:09:42



> Can you arrange for script B to write a file after it has done "a few
> things"?  Then you can do something like
> script_B &
> until [ -f done_it ]; do
>   sleep 1
> done
> rm done_it

Just curious: Can this be done with signals?  In particular, can a trapped
signal break out of a loop, like this

trap "break" USR1
script_B &
while true; do
  sleep 1
done

(assuming script_B does `kill -USR1 $$PPID` at some point)?

--

 
 
 

unblocking a parent shell after it launches a subshell

Post by David Thompso » Fri, 12 Apr 2002 03:31:27




> > Can you arrange for script B to write a file after it has done "a few
> > things"?  Then you can do something like
> > script_B &
> > until [ -f done_it ]; do
> >   sleep 1
> > done
> > rm done_it

> Just curious: Can this be done with signals?  In particular, can a trapped
> signal break out of a loop, like this

> trap "break" USR1
> script_B &
> while true; do
>   sleep 1
> done

Don't know about the break, did you try it?

I've done something like this before,

  WAITING=true
  trap "WAITING=false" USR1
  script_B &
  while $WAITING ; do
    sleep 1
  done

--
David Thompson

 
 
 

unblocking a parent shell after it launches a subshell

Post by those who know me have no need of my nam » Fri, 12 Apr 2002 03:10:36



Quote:>I want script A to wait on script B until B has done a few things.
>But, then I want script A to proceed leaving B to finish. How is this
>possible?

use a lock file, e.g.,

a:
  touch lock.file
  ./b &
  while (-f lock.file)
    sleep 1
  end
  ...

b:
  ...
  rm lock.file
  ...

--
bringing you boring signatures for 17 years

 
 
 

unblocking a parent shell after it launches a subshell

Post by laura fairhe » Fri, 12 Apr 2002 04:00:17




>> Can you arrange for script B to write a file after it has done "a few
>> things"?  Then you can do something like
>> script_B &
>> until [ -f done_it ]; do
>>   sleep 1
>> done
>> rm done_it

>Just curious: Can this be done with signals?  In particular, can a trapped
>signal break out of a loop, like this

>trap "break" USR1
>script_B &
>while true; do
>  sleep 1
>done

>(assuming script_B does `kill -USR1 $$PPID` at some point)?

You can do this in a bourne shell, and it is the natural solution
you could use a flag variable instead of 'break' for the general
case of polling whenever you like for the state of the 2nd task;

~~~~~~~~~main
#!/bin/sh
trap 'task2flag=1' USR1
task2flag=0
_PPID=$$
export _PPID
./script2&
while true
do
  [ $task2flag -eq 1 ] && break
  echo task1 doing some stuff...
  sleep 1
done
echo task2 completed
~~~~~~~~~script2
#!/bin/sh
sleep 10
kill -USR1 $_PPID
~~~~~~~~~

However C-Shell can't do this, if you really must script in
C-Shell you're stuck with the files solution or maybe an
unreliable grepping of 'ps';

From 'Csh Programming Considered Harmful'

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

3. SIGNALS

In the csh, all you can do with signals is trap SIGINT.  In the Bourne
shell, you can trap any signal, or the end-of-program exit.    For example,
to blow away a tempfile on any of a variety of signals:

    $ trap 'rm -f /usr/adm/tmp/i$$ ;
            echo "ERROR: abnormal exit";
            exit' 1 2 3 15

    $ trap 'rm tmp.$$' 0   # on program exit

>--


bestwishes

--

                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492