Here's an approach that doesn't involve looping and checking if the other process is still alive, or calling train_v1.py
in a manner different from what you'd normally do:
$ python train_v1.py
^Z
[1]+ Stopped python train_v1.py
$ % && python train_v2.py
The ^Z
is me pressing Ctrl+Z while the process is running to sleep train_v1.py
through sending it a SIGTSTP
signal. Then, I tell the shell to wake it with %
, using it as a command to which I can add the && python train_v2.py
at the end. This makes it behave just as if you'd done python train_v1.py && python train_v2.py
from the very beginning.
Instead of %
, you can also use fg
. It's the same thing. If you want to learn more about these types of features of the shell, you can read about them in the "JOB CONTROL" section of bash's manpage.
EDIT: How to keep adding to the queue
As pointed out by jamesdlin in a comment, if you try to continue the pattern to add train_v3.py
for example before v2 starts, you'll find that you can't:
$ % && python train_v2.py
^Z
[1]+ Stopped python train_v1.py
Only train_v1.py
gets stopped because train_v2.py
hasn't started, and you can't stop/suspend/sleep something that hasn't even started.
$ % && python train_v3.py
would result in the same as
python train_v1.py && python train_v3.py
because %
corresponds to the last suspended process. Instead of trying to add v3
like that, one should instead use history:
$ !! && python train_v3.py
% && python train_v2.py && python train_v3.py
One can do history expansion like above, or recall the last command with a keybinding (like up) and add v3 to the end.
$ % && python train_v2.py && python train_v3.py
That's something that can be repeated to add more to the pipeline.
$ !! && python train_v3.py
% && python train_v2.py && python train_v3.py
^Z
[1]+ Stopped python train_v1.py
$ !! && python train_v4.py
% && python train_v2.py && python train_v3.py && python train_v4.py
python train_v1.py && python train_v2py
, you'll runpython train_v1.py
and only after that completes successfully will it runpython train_v2.py
. That sounds like what you're after.python v1; python v2
python train_v1.py
, you don't yet know whether you'll have a second program to run (or, similarly, what exactly the second command will be)? And that you can't/are not willing to simply startpython foo && python bar && python baz
and then just ensure that the programs you are still writing will be namedbar
andbaz
? (Noting that the shell won't complain if, when you runfoo && bar
,bar
doesn't exist yet; it will only complain ifbar
does not exist whenfoo
terminates).&&
. This question is clearly about achieving the same effect after the first program has already been started.