How to plan a task to run after another already running task in bash? [duplicate]












11
















This question already has an answer here:




  • Queue a task in a running shell

    3 answers



  • Queue up commands while one command is being executed

    3 answers




I'm looking for something like command1 ; command2 i.e. how to run command2 after command1 but I'd like to plan execution of command2 when command1 is already running.



It can be solved by just typing the command2 and confirming by Enter supposed that the command1 is not consuming standard input and that the command1 doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1 output).










share|improve this question















marked as duplicate by muru, Scott, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 15 '18 at 11:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    11
















    This question already has an answer here:




    • Queue a task in a running shell

      3 answers



    • Queue up commands while one command is being executed

      3 answers




    I'm looking for something like command1 ; command2 i.e. how to run command2 after command1 but I'd like to plan execution of command2 when command1 is already running.



    It can be solved by just typing the command2 and confirming by Enter supposed that the command1 is not consuming standard input and that the command1 doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1 output).










    share|improve this question















    marked as duplicate by muru, Scott, Kusalananda bash
    Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Dec 15 '18 at 11:11


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      11












      11








      11


      2







      This question already has an answer here:




      • Queue a task in a running shell

        3 answers



      • Queue up commands while one command is being executed

        3 answers




      I'm looking for something like command1 ; command2 i.e. how to run command2 after command1 but I'd like to plan execution of command2 when command1 is already running.



      It can be solved by just typing the command2 and confirming by Enter supposed that the command1 is not consuming standard input and that the command1 doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1 output).










      share|improve this question

















      This question already has an answer here:




      • Queue a task in a running shell

        3 answers



      • Queue up commands while one command is being executed

        3 answers




      I'm looking for something like command1 ; command2 i.e. how to run command2 after command1 but I'd like to plan execution of command2 when command1 is already running.



      It can be solved by just typing the command2 and confirming by Enter supposed that the command1 is not consuming standard input and that the command1 doesn't produce to much text on output making it impractical to type (typed characters are blended with the command1 output).





      This question already has an answer here:




      • Queue a task in a running shell

        3 answers



      • Queue up commands while one command is being executed

        3 answers








      bash job-control jobs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 15 '18 at 1:14







      czerny

















      asked Dec 14 '18 at 10:56









      czernyczerny

      5151312




      5151312




      marked as duplicate by muru, Scott, Kusalananda bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 15 '18 at 11:11


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by muru, Scott, Kusalananda bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 15 '18 at 11:11


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          5 Answers
          5






          active

          oldest

          votes


















          8














          Generally what I do is: Ctrl+Z fg && command2





          1. Ctrl+Z to pause it and let you type more in the shell.

          2. Optionally bg, to resume command1 in the background while you type out command2.


          3. fg && command2 to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute ; or || for the && if you so desire.






          share|improve this answer





















          • 1





            Stick a bg in there and win!

            – Joshua
            Dec 14 '18 at 20:58






          • 2





            @Joshua okay, what do I win now :P

            – The Guy with The Hat
            Dec 14 '18 at 21:13



















          16
















          1. start a command



            command1


          2. pause the execution by pressing Ctrl+Z



          3. find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing



            jobs



          4. let command1 continue in background



            bg



          5. plan execution of command2 to await finish of command1



            wait -n <command1 job number> ; command2



          Documentation Job Control Builtins






          share|improve this answer
























          • You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.

            – sudodus
            Dec 14 '18 at 11:52






          • 1





            @sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers

            – czerny
            Dec 14 '18 at 11:59






          • 6





            In simple cases I sometimes control-z then use fg ; command. Or even fg && command to only run the 2nd command / pipeline if the first one was successful. (You can still use bg while thinking / typing, if it doesn't spew text on the terminal.)

            – Peter Cordes
            Dec 14 '18 at 13:32






          • 2





            I'm a bit confused as to what this approach offers beyond the typical command1; command2 syntax. Could you please clarify this for me?

            – Haxiel
            Dec 14 '18 at 17:32






          • 1





            command1 & proceed_after=$! then when you've got the next command figured out wait -n $proceed_after; command 2

            – studog
            Dec 14 '18 at 18:27



















          3














          There are several alternatives.





          • With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.



            command1 & command2



          • With two ampersands, 'logical and', the second program will start only if the first program finished successfully.



            command1 && command2



          • With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.



            command1 ; command2


          • You can use wait <PID> to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).



          • Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by ps. When it is no longer found, the second command will be started.



            This demo example for bash checks if top is running via the PID, and runs the command



            echo "*** $USER, I am ready now ***"


            if/when top is no longer running.



            pid=$(ps -A|grep top|cut -d ' ' -f 1); 
            while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
            if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
            echo "*** $USER, I am ready now ***"







          share|improve this answer

































            0














            The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:



            command1 & command2


            & is not a connector between these two commands. It can be used with command1 alone to have it started in the background:



            command1 &


            And this is right syntax for starting both commands in the background:



            command1 & command2 &





            share|improve this answer
























            • Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.

              – czerny
              Dec 14 '18 at 11:22











            • @czerny So you want the second one to start while the first one is still running, when it's already finished, or?

              – Tomasz
              Dec 14 '18 at 11:25











            • I'd like the second command to be started when the first command finishes

              – czerny
              Dec 14 '18 at 11:53






            • 1





              @czerny Then just use ;. Or do you want to do something while the first one is still running?

              – Tomasz
              Dec 14 '18 at 12:54



















            -3














            If you have a choice to use two terminals, here's what you can do:




            • Assuming you are already running command1 in a terminal (let's call it terminal1)

            • Start a new terminal (let's call it terminal2)

            • Find process ID of command1 (let's call it command1_pid). In terminal2, run command ps -ef | grep <command1> (you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring).

            • Run command wait <command1_pid> ; command2


            Basically, wait is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait command.






            share|improve this answer



















            • 2





              It doesn't seem to work for me. Running wait in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell.

              – czerny
              Dec 14 '18 at 11:48






            • 1





              @czerny That's normal, I don't think this answer was tested. POSIX wait system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait and waitpid return with ECHILD error status in this case.

              – Peter Cordes
              Dec 14 '18 at 13:38


















            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            8














            Generally what I do is: Ctrl+Z fg && command2





            1. Ctrl+Z to pause it and let you type more in the shell.

            2. Optionally bg, to resume command1 in the background while you type out command2.


            3. fg && command2 to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute ; or || for the && if you so desire.






            share|improve this answer





















            • 1





              Stick a bg in there and win!

              – Joshua
              Dec 14 '18 at 20:58






            • 2





              @Joshua okay, what do I win now :P

              – The Guy with The Hat
              Dec 14 '18 at 21:13
















            8














            Generally what I do is: Ctrl+Z fg && command2





            1. Ctrl+Z to pause it and let you type more in the shell.

            2. Optionally bg, to resume command1 in the background while you type out command2.


            3. fg && command2 to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute ; or || for the && if you so desire.






            share|improve this answer





















            • 1





              Stick a bg in there and win!

              – Joshua
              Dec 14 '18 at 20:58






            • 2





              @Joshua okay, what do I win now :P

              – The Guy with The Hat
              Dec 14 '18 at 21:13














            8












            8








            8







            Generally what I do is: Ctrl+Z fg && command2





            1. Ctrl+Z to pause it and let you type more in the shell.

            2. Optionally bg, to resume command1 in the background while you type out command2.


            3. fg && command2 to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute ; or || for the && if you so desire.






            share|improve this answer















            Generally what I do is: Ctrl+Z fg && command2





            1. Ctrl+Z to pause it and let you type more in the shell.

            2. Optionally bg, to resume command1 in the background while you type out command2.


            3. fg && command2 to resume command1 in the foreground and queue up command2 afterwards if command1 succeeds. You can of course substitute ; or || for the && if you so desire.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 14 '18 at 22:08

























            answered Dec 14 '18 at 16:09









            The Guy with The HatThe Guy with The Hat

            20616




            20616








            • 1





              Stick a bg in there and win!

              – Joshua
              Dec 14 '18 at 20:58






            • 2





              @Joshua okay, what do I win now :P

              – The Guy with The Hat
              Dec 14 '18 at 21:13














            • 1





              Stick a bg in there and win!

              – Joshua
              Dec 14 '18 at 20:58






            • 2





              @Joshua okay, what do I win now :P

              – The Guy with The Hat
              Dec 14 '18 at 21:13








            1




            1





            Stick a bg in there and win!

            – Joshua
            Dec 14 '18 at 20:58





            Stick a bg in there and win!

            – Joshua
            Dec 14 '18 at 20:58




            2




            2





            @Joshua okay, what do I win now :P

            – The Guy with The Hat
            Dec 14 '18 at 21:13





            @Joshua okay, what do I win now :P

            – The Guy with The Hat
            Dec 14 '18 at 21:13













            16
















            1. start a command



              command1


            2. pause the execution by pressing Ctrl+Z



            3. find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing



              jobs



            4. let command1 continue in background



              bg



            5. plan execution of command2 to await finish of command1



              wait -n <command1 job number> ; command2



            Documentation Job Control Builtins






            share|improve this answer
























            • You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.

              – sudodus
              Dec 14 '18 at 11:52






            • 1





              @sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers

              – czerny
              Dec 14 '18 at 11:59






            • 6





              In simple cases I sometimes control-z then use fg ; command. Or even fg && command to only run the 2nd command / pipeline if the first one was successful. (You can still use bg while thinking / typing, if it doesn't spew text on the terminal.)

              – Peter Cordes
              Dec 14 '18 at 13:32






            • 2





              I'm a bit confused as to what this approach offers beyond the typical command1; command2 syntax. Could you please clarify this for me?

              – Haxiel
              Dec 14 '18 at 17:32






            • 1





              command1 & proceed_after=$! then when you've got the next command figured out wait -n $proceed_after; command 2

              – studog
              Dec 14 '18 at 18:27
















            16
















            1. start a command



              command1


            2. pause the execution by pressing Ctrl+Z



            3. find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing



              jobs



            4. let command1 continue in background



              bg



            5. plan execution of command2 to await finish of command1



              wait -n <command1 job number> ; command2



            Documentation Job Control Builtins






            share|improve this answer
























            • You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.

              – sudodus
              Dec 14 '18 at 11:52






            • 1





              @sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers

              – czerny
              Dec 14 '18 at 11:59






            • 6





              In simple cases I sometimes control-z then use fg ; command. Or even fg && command to only run the 2nd command / pipeline if the first one was successful. (You can still use bg while thinking / typing, if it doesn't spew text on the terminal.)

              – Peter Cordes
              Dec 14 '18 at 13:32






            • 2





              I'm a bit confused as to what this approach offers beyond the typical command1; command2 syntax. Could you please clarify this for me?

              – Haxiel
              Dec 14 '18 at 17:32






            • 1





              command1 & proceed_after=$! then when you've got the next command figured out wait -n $proceed_after; command 2

              – studog
              Dec 14 '18 at 18:27














            16












            16








            16









            1. start a command



              command1


            2. pause the execution by pressing Ctrl+Z



            3. find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing



              jobs



            4. let command1 continue in background



              bg



            5. plan execution of command2 to await finish of command1



              wait -n <command1 job number> ; command2



            Documentation Job Control Builtins






            share|improve this answer















            1. start a command



              command1


            2. pause the execution by pressing Ctrl+Z



            3. find the job number of the paused command (it's usually already printed to console when to console when to command is paused) by executing



              jobs



            4. let command1 continue in background



              bg



            5. plan execution of command2 to await finish of command1



              wait -n <command1 job number> ; command2



            Documentation Job Control Builtins







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 14 '18 at 11:47









            czernyczerny

            5151312




            5151312













            • You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.

              – sudodus
              Dec 14 '18 at 11:52






            • 1





              @sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers

              – czerny
              Dec 14 '18 at 11:59






            • 6





              In simple cases I sometimes control-z then use fg ; command. Or even fg && command to only run the 2nd command / pipeline if the first one was successful. (You can still use bg while thinking / typing, if it doesn't spew text on the terminal.)

              – Peter Cordes
              Dec 14 '18 at 13:32






            • 2





              I'm a bit confused as to what this approach offers beyond the typical command1; command2 syntax. Could you please clarify this for me?

              – Haxiel
              Dec 14 '18 at 17:32






            • 1





              command1 & proceed_after=$! then when you've got the next command figured out wait -n $proceed_after; command 2

              – studog
              Dec 14 '18 at 18:27



















            • You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.

              – sudodus
              Dec 14 '18 at 11:52






            • 1





              @sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers

              – czerny
              Dec 14 '18 at 11:59






            • 6





              In simple cases I sometimes control-z then use fg ; command. Or even fg && command to only run the 2nd command / pipeline if the first one was successful. (You can still use bg while thinking / typing, if it doesn't spew text on the terminal.)

              – Peter Cordes
              Dec 14 '18 at 13:32






            • 2





              I'm a bit confused as to what this approach offers beyond the typical command1; command2 syntax. Could you please clarify this for me?

              – Haxiel
              Dec 14 '18 at 17:32






            • 1





              command1 & proceed_after=$! then when you've got the next command figured out wait -n $proceed_after; command 2

              – studog
              Dec 14 '18 at 18:27

















            You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.

            – sudodus
            Dec 14 '18 at 11:52





            You can 'accept' your own answer (click the tick icon) into order to show other users that there is a good answer to your question.

            – sudodus
            Dec 14 '18 at 11:52




            1




            1





            @sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers

            – czerny
            Dec 14 '18 at 11:59





            @sudodus Own answer can be accepted at earliest 2 days after the question was asked. stackoverflow.blog/2009/01/06/accept-your-own-answers

            – czerny
            Dec 14 '18 at 11:59




            6




            6





            In simple cases I sometimes control-z then use fg ; command. Or even fg && command to only run the 2nd command / pipeline if the first one was successful. (You can still use bg while thinking / typing, if it doesn't spew text on the terminal.)

            – Peter Cordes
            Dec 14 '18 at 13:32





            In simple cases I sometimes control-z then use fg ; command. Or even fg && command to only run the 2nd command / pipeline if the first one was successful. (You can still use bg while thinking / typing, if it doesn't spew text on the terminal.)

            – Peter Cordes
            Dec 14 '18 at 13:32




            2




            2





            I'm a bit confused as to what this approach offers beyond the typical command1; command2 syntax. Could you please clarify this for me?

            – Haxiel
            Dec 14 '18 at 17:32





            I'm a bit confused as to what this approach offers beyond the typical command1; command2 syntax. Could you please clarify this for me?

            – Haxiel
            Dec 14 '18 at 17:32




            1




            1





            command1 & proceed_after=$! then when you've got the next command figured out wait -n $proceed_after; command 2

            – studog
            Dec 14 '18 at 18:27





            command1 & proceed_after=$! then when you've got the next command figured out wait -n $proceed_after; command 2

            – studog
            Dec 14 '18 at 18:27











            3














            There are several alternatives.





            • With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.



              command1 & command2



            • With two ampersands, 'logical and', the second program will start only if the first program finished successfully.



              command1 && command2



            • With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.



              command1 ; command2


            • You can use wait <PID> to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).



            • Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by ps. When it is no longer found, the second command will be started.



              This demo example for bash checks if top is running via the PID, and runs the command



              echo "*** $USER, I am ready now ***"


              if/when top is no longer running.



              pid=$(ps -A|grep top|cut -d ' ' -f 1); 
              while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
              if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
              echo "*** $USER, I am ready now ***"







            share|improve this answer






























              3














              There are several alternatives.





              • With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.



                command1 & command2



              • With two ampersands, 'logical and', the second program will start only if the first program finished successfully.



                command1 && command2



              • With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.



                command1 ; command2


              • You can use wait <PID> to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).



              • Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by ps. When it is no longer found, the second command will be started.



                This demo example for bash checks if top is running via the PID, and runs the command



                echo "*** $USER, I am ready now ***"


                if/when top is no longer running.



                pid=$(ps -A|grep top|cut -d ' ' -f 1); 
                while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
                if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
                echo "*** $USER, I am ready now ***"







              share|improve this answer




























                3












                3








                3







                There are several alternatives.





                • With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.



                  command1 & command2



                • With two ampersands, 'logical and', the second program will start only if the first program finished successfully.



                  command1 && command2



                • With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.



                  command1 ; command2


                • You can use wait <PID> to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).



                • Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by ps. When it is no longer found, the second command will be started.



                  This demo example for bash checks if top is running via the PID, and runs the command



                  echo "*** $USER, I am ready now ***"


                  if/when top is no longer running.



                  pid=$(ps -A|grep top|cut -d ' ' -f 1); 
                  while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
                  if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
                  echo "*** $USER, I am ready now ***"







                share|improve this answer















                There are several alternatives.





                • With one ampersand, 'push to background', the second program starts after the first one starts, but they will probably run alongside each other.



                  command1 & command2



                • With two ampersands, 'logical and', the second program will start only if the first program finished successfully.



                  command1 && command2



                • With a semicolon separating the commands in the command line, the the second program will start, when the first program finished, even if it failed or was interrupted.



                  command1 ; command2


                • You can use wait <PID> to wait for the first command to finish, if it is already running from the same shell (in the same terminal window).



                • Otherwise if the first command is already running from another shell (in another window), you can use a small while loop using ps to check if the PID is still found by ps. When it is no longer found, the second command will be started.



                  This demo example for bash checks if top is running via the PID, and runs the command



                  echo "*** $USER, I am ready now ***"


                  if/when top is no longer running.



                  pid=$(ps -A|grep top|cut -d ' ' -f 1); 
                  while [ "$pid" != "" ]; do ps -A|grep "$pid" > /dev/null;
                  if [ $? -eq 0 ]; then sleep 5;else pid=""; fi; done;
                  echo "*** $USER, I am ready now ***"








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 15 '18 at 0:07

























                answered Dec 14 '18 at 11:37









                sudodussudodus

                1,24816




                1,24816























                    0














                    The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:



                    command1 & command2


                    & is not a connector between these two commands. It can be used with command1 alone to have it started in the background:



                    command1 &


                    And this is right syntax for starting both commands in the background:



                    command1 & command2 &





                    share|improve this answer
























                    • Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.

                      – czerny
                      Dec 14 '18 at 11:22











                    • @czerny So you want the second one to start while the first one is still running, when it's already finished, or?

                      – Tomasz
                      Dec 14 '18 at 11:25











                    • I'd like the second command to be started when the first command finishes

                      – czerny
                      Dec 14 '18 at 11:53






                    • 1





                      @czerny Then just use ;. Or do you want to do something while the first one is still running?

                      – Tomasz
                      Dec 14 '18 at 12:54
















                    0














                    The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:



                    command1 & command2


                    & is not a connector between these two commands. It can be used with command1 alone to have it started in the background:



                    command1 &


                    And this is right syntax for starting both commands in the background:



                    command1 & command2 &





                    share|improve this answer
























                    • Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.

                      – czerny
                      Dec 14 '18 at 11:22











                    • @czerny So you want the second one to start while the first one is still running, when it's already finished, or?

                      – Tomasz
                      Dec 14 '18 at 11:25











                    • I'd like the second command to be started when the first command finishes

                      – czerny
                      Dec 14 '18 at 11:53






                    • 1





                      @czerny Then just use ;. Or do you want to do something while the first one is still running?

                      – Tomasz
                      Dec 14 '18 at 12:54














                    0












                    0








                    0







                    The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:



                    command1 & command2


                    & is not a connector between these two commands. It can be used with command1 alone to have it started in the background:



                    command1 &


                    And this is right syntax for starting both commands in the background:



                    command1 & command2 &





                    share|improve this answer













                    The standard way is just to let command 1 start in the background and then have command 2 start either in the background or in the foreground. The full command is this:



                    command1 & command2


                    & is not a connector between these two commands. It can be used with command1 alone to have it started in the background:



                    command1 &


                    And this is right syntax for starting both commands in the background:



                    command1 & command2 &






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 14 '18 at 11:07









                    TomaszTomasz

                    9,19352965




                    9,19352965













                    • Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.

                      – czerny
                      Dec 14 '18 at 11:22











                    • @czerny So you want the second one to start while the first one is still running, when it's already finished, or?

                      – Tomasz
                      Dec 14 '18 at 11:25











                    • I'd like the second command to be started when the first command finishes

                      – czerny
                      Dec 14 '18 at 11:53






                    • 1





                      @czerny Then just use ;. Or do you want to do something while the first one is still running?

                      – Tomasz
                      Dec 14 '18 at 12:54



















                    • Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.

                      – czerny
                      Dec 14 '18 at 11:22











                    • @czerny So you want the second one to start while the first one is still running, when it's already finished, or?

                      – Tomasz
                      Dec 14 '18 at 11:25











                    • I'd like the second command to be started when the first command finishes

                      – czerny
                      Dec 14 '18 at 11:53






                    • 1





                      @czerny Then just use ;. Or do you want to do something while the first one is still running?

                      – Tomasz
                      Dec 14 '18 at 12:54

















                    Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.

                    – czerny
                    Dec 14 '18 at 11:22





                    Thank you for your answer. I don't mind if commands are running in background or foreground. I'd like to run them serially (one starts when the previous one ends) and I'd like to plan the execution of the second one when the first one is already running.

                    – czerny
                    Dec 14 '18 at 11:22













                    @czerny So you want the second one to start while the first one is still running, when it's already finished, or?

                    – Tomasz
                    Dec 14 '18 at 11:25





                    @czerny So you want the second one to start while the first one is still running, when it's already finished, or?

                    – Tomasz
                    Dec 14 '18 at 11:25













                    I'd like the second command to be started when the first command finishes

                    – czerny
                    Dec 14 '18 at 11:53





                    I'd like the second command to be started when the first command finishes

                    – czerny
                    Dec 14 '18 at 11:53




                    1




                    1





                    @czerny Then just use ;. Or do you want to do something while the first one is still running?

                    – Tomasz
                    Dec 14 '18 at 12:54





                    @czerny Then just use ;. Or do you want to do something while the first one is still running?

                    – Tomasz
                    Dec 14 '18 at 12:54











                    -3














                    If you have a choice to use two terminals, here's what you can do:




                    • Assuming you are already running command1 in a terminal (let's call it terminal1)

                    • Start a new terminal (let's call it terminal2)

                    • Find process ID of command1 (let's call it command1_pid). In terminal2, run command ps -ef | grep <command1> (you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring).

                    • Run command wait <command1_pid> ; command2


                    Basically, wait is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait command.






                    share|improve this answer



















                    • 2





                      It doesn't seem to work for me. Running wait in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell.

                      – czerny
                      Dec 14 '18 at 11:48






                    • 1





                      @czerny That's normal, I don't think this answer was tested. POSIX wait system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait and waitpid return with ECHILD error status in this case.

                      – Peter Cordes
                      Dec 14 '18 at 13:38
















                    -3














                    If you have a choice to use two terminals, here's what you can do:




                    • Assuming you are already running command1 in a terminal (let's call it terminal1)

                    • Start a new terminal (let's call it terminal2)

                    • Find process ID of command1 (let's call it command1_pid). In terminal2, run command ps -ef | grep <command1> (you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring).

                    • Run command wait <command1_pid> ; command2


                    Basically, wait is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait command.






                    share|improve this answer



















                    • 2





                      It doesn't seem to work for me. Running wait in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell.

                      – czerny
                      Dec 14 '18 at 11:48






                    • 1





                      @czerny That's normal, I don't think this answer was tested. POSIX wait system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait and waitpid return with ECHILD error status in this case.

                      – Peter Cordes
                      Dec 14 '18 at 13:38














                    -3












                    -3








                    -3







                    If you have a choice to use two terminals, here's what you can do:




                    • Assuming you are already running command1 in a terminal (let's call it terminal1)

                    • Start a new terminal (let's call it terminal2)

                    • Find process ID of command1 (let's call it command1_pid). In terminal2, run command ps -ef | grep <command1> (you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring).

                    • Run command wait <command1_pid> ; command2


                    Basically, wait is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait command.






                    share|improve this answer













                    If you have a choice to use two terminals, here's what you can do:




                    • Assuming you are already running command1 in a terminal (let's call it terminal1)

                    • Start a new terminal (let's call it terminal2)

                    • Find process ID of command1 (let's call it command1_pid). In terminal2, run command ps -ef | grep <command1> (you might want to properly form the filter string in grep command so that you do not accidently find another process having as a substring).

                    • Run command wait <command1_pid> ; command2


                    Basically, wait is a bash built-in command that waits until the provided PIDs exit and then return. You are simply delegating your waiting for command1 to finish, on to bash's wait command.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 14 '18 at 11:31









                    PrasannaPrasanna

                    993




                    993








                    • 2





                      It doesn't seem to work for me. Running wait in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell.

                      – czerny
                      Dec 14 '18 at 11:48






                    • 1





                      @czerny That's normal, I don't think this answer was tested. POSIX wait system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait and waitpid return with ECHILD error status in this case.

                      – Peter Cordes
                      Dec 14 '18 at 13:38














                    • 2





                      It doesn't seem to work for me. Running wait in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell.

                      – czerny
                      Dec 14 '18 at 11:48






                    • 1





                      @czerny That's normal, I don't think this answer was tested. POSIX wait system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait and waitpid return with ECHILD error status in this case.

                      – Peter Cordes
                      Dec 14 '18 at 13:38








                    2




                    2





                    It doesn't seem to work for me. Running wait in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell.

                    – czerny
                    Dec 14 '18 at 11:48





                    It doesn't seem to work for me. Running wait in different terminal outputs following error message: bash: wait: pid 19531 is not a child of this shell.

                    – czerny
                    Dec 14 '18 at 11:48




                    1




                    1





                    @czerny That's normal, I don't think this answer was tested. POSIX wait system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait and waitpid return with ECHILD error status in this case.

                    – Peter Cordes
                    Dec 14 '18 at 13:38





                    @czerny That's normal, I don't think this answer was tested. POSIX wait system calls only work on child processes, so one bash process can't wait for the children of a different instance of bash. man7.org/linux/man-pages/man2/wait.2.html#ERRORS says wait and waitpid return with ECHILD error status in this case.

                    – Peter Cordes
                    Dec 14 '18 at 13:38



                    Popular posts from this blog

                    Сан-Квентин

                    Алькесар

                    Josef Freinademetz