# \[OT\] bash "wait for previous to finish" option?

**URL:** <https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214>\
**Category:** Ruby\
**Created:** [December 14, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214 "2009-12-14T23:57:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 14, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214/1 "2009-12-14T23:57:47Z")

</div>

Anybody know if something like this exists.

two terminals open

1. hack hack hack [start long running process]
2. hack hack [type in the name of a command which should be run  
immediately after long running process in terminal one finishes–and  
somehow it will start immediately afterward].

[windows compatible would be nice].

Anything like that out there?  
Thanks.  
-r

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 15, 2009, 12:35am UTC](https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214/2 "2009-12-15T00:35:02Z")

</div>

On Tue, Dec 15, 2009 at 4:27 AM, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Anybody know if something like this exists.
> 
> two terminals open
> 
> 1. hack hack hack [start long running process]
> 2. hack hack [type in the name of a command which should be run  
> immediately after long running process in terminal one finishes–and  
> somehow it will start immediately afterward].

wait should do it.

you can also go back to terminal 1, hit ^Z, then run fg && command

martin

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 15, 2009, 12:47am UTC](https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214/3 "2009-12-15T00:47:19Z")

</div>

On 2009-12-14, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anything like that out there?

In the first terminal window:

touch /tmp/waitforme

In the second

while ! test -e /tmp/waitforme  
do  
sleep 1  
done

This might be more topical in comp.unix.shell. 🙂

-s

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 15, 2009, 2:32am UTC](https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214/4 "2009-12-15T02:32:06Z")

</div>

On Monday 14 December 2009 04:57:49 pm Roger P. wrote:

> two terminals open
> 
> 1. hack hack hack [start long running process]
> 2. hack hack [type in the name of a command which should be run  
> immediately after long running process in terminal one finishes–and  
> somehow it will start immediately afterward].

Definitely offtopic, but there a lot of answers, depending on what you  
actually want.

First, are you just talking about running one process after another?  
That’s  
easy, semicolon, or && if you care about the return value. For example:

pv foo.tar | bzip2 -v9 \> foo.tar.bz2 && rm foo.tar

Now, if you do care about them being in separate terminals, you need  
some way  
to signal the other terminal, and people mentioned various ways to do  
that.  
Probably the least hacky way might be something like:

mkfifo pipe  
tar -cjpSf foo.tar.bz2 foo && echo \> pipe

Then, in terminal two:

cat pipe && do\_some\_other\_command

If your problem is that you already started the long-running process,  
and now  
you want to do something when it’s done, that’s done with wait. Easiest  
way,  
if the long-running job ignores terminal input, just type the name of  
the next  
command (and enter) and it’ll probably go to bash. More thorough way, go  
to  
the terminal where you started it and suspend it with ctrl+Z. Then type  
‘bg’  
to allow it to continue in the background, and pay attention to the job  
number. Then you don’t even need a pid, just wait %n, where n is the job  
number.

If you need to safely do this and then trigger something in that other  
window,  
you can probably figure out how to combine the above into something  
useful.  
Unfortunately, most of the time I think of things like this, I figure by  
the  
time I remember all my Unix tricks, the “long-running” job (of maybe a  
minute  
or two) will be done.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 15, 2009, 12:48am UTC](https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214/5 "2009-12-15T00:48:30Z")

</div>

On Dec 14, 2009, at 2:57 PM, Roger P. wrote:

> Anything like that out there?

$ date; sleep 60 &  
Mon Dec 14 15:37:56 PST 2009  
[1] 32459  
$ while kill -0 32459; do sleep 1; done; date; echo DONE  
ksh: kill: 32459: No such process  
Mon Dec 14 15:38:57 PST 2009  
DONE  
[1] + Done sleep 60  
$

“sleep 60 &” emulates your long running process, no matter from what  
terminal as long as you can find out its process id. “echo DONE” will be  
executed right after process 32459 (sleep 60) terminates. If you do not  
want to see the error from kill, redirect its stderr to /dev/null (for  
the above, kill -0 32459 2\>/dev/null).

No windows, sorry.

Gennady.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 15, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/ot-bash-wait-for-previous-to-finish-option/180214/6 "2009-12-15T16:42:19Z")

</div>

> If you need to safely do this and then trigger something in that other  
> window,

Thanks for your and everyone else’s responses. Since ruby has a #waitpid  
on both windows and linux, I think I’m going to hack up something that  
has a use like this:

$ wait\_for.rb “something that describes the other process, like a unique  
command line parameter, so I can lookup its pid and wait on it” then do  
this other command

Thanks!  
-r
