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

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

On Tue, Dec 15, 2009 at 4:27 AM, Roger P. [email protected]
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

On 2009-12-14, Roger P. [email protected] 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. :slight_smile:

-s

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.

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.

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