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











up vote
10
down vote

favorite
2












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
























  • Possible duplicate of Queue up commands while one command is being executed
    – muru
    16 mins ago










  • And unix.stackexchange.com/questions/132068/…
    – muru
    16 mins ago















up vote
10
down vote

favorite
2












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
























  • Possible duplicate of Queue up commands while one command is being executed
    – muru
    16 mins ago










  • And unix.stackexchange.com/questions/132068/…
    – muru
    16 mins ago













up vote
10
down vote

favorite
2









up vote
10
down vote

favorite
2






2





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















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).







bash job-control jobs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 15 hours ago









czerny

4881312




4881312












  • Possible duplicate of Queue up commands while one command is being executed
    – muru
    16 mins ago










  • And unix.stackexchange.com/questions/132068/…
    – muru
    16 mins ago


















  • Possible duplicate of Queue up commands while one command is being executed
    – muru
    16 mins ago










  • And unix.stackexchange.com/questions/132068/…
    – muru
    16 mins ago
















Possible duplicate of Queue up commands while one command is being executed
– muru
16 mins ago




Possible duplicate of Queue up commands while one command is being executed
– muru
16 mins ago












And unix.stackexchange.com/questions/132068/…
– muru
16 mins ago




And unix.stackexchange.com/questions/132068/…
– muru
16 mins ago










5 Answers
5






active

oldest

votes

















up vote
14
down vote















  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
    14 hours ago






  • 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
    14 hours ago






  • 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
    13 hours ago






  • 1




    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
    9 hours ago






  • 1




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


















up vote
7
down vote













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
    5 hours ago






  • 1




    @Joshua okay, what do I win now :P
    – The Guy with The Hat
    5 hours ago


















up vote
2
down vote













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






























    up vote
    1
    down vote













    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
      15 hours ago










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










    • I'd like the second command to be started when the first command finishes
      – czerny
      14 hours ago






    • 1




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


















    up vote
    -2
    down vote













    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








    New contributor




    Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.














    • 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
      14 hours ago










    • @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
      13 hours ago











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487955%2fhow-to-plan-a-task-to-run-after-another-already-running-task-in-bash%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    14
    down vote















    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
      14 hours ago






    • 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
      14 hours ago






    • 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
      13 hours ago






    • 1




      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
      9 hours ago






    • 1




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















    up vote
    14
    down vote















    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
      14 hours ago






    • 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
      14 hours ago






    • 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
      13 hours ago






    • 1




      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
      9 hours ago






    • 1




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













    up vote
    14
    down vote










    up vote
    14
    down vote











    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 14 hours ago









    czerny

    4881312




    4881312












    • 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
      14 hours ago






    • 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
      14 hours ago






    • 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
      13 hours ago






    • 1




      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
      9 hours ago






    • 1




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


















    • 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
      14 hours ago






    • 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
      14 hours ago






    • 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
      13 hours ago






    • 1




      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
      9 hours ago






    • 1




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
















    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
    14 hours ago




    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
    14 hours ago




    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
    14 hours ago




    @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
    14 hours ago




    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
    13 hours ago




    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
    13 hours ago




    1




    1




    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
    9 hours ago




    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
    9 hours ago




    1




    1




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




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












    up vote
    7
    down vote













    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
      5 hours ago






    • 1




      @Joshua okay, what do I win now :P
      – The Guy with The Hat
      5 hours ago















    up vote
    7
    down vote













    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
      5 hours ago






    • 1




      @Joshua okay, what do I win now :P
      – The Guy with The Hat
      5 hours ago













    up vote
    7
    down vote










    up vote
    7
    down vote









    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 4 hours ago

























    answered 10 hours ago









    The Guy with The Hat

    1816




    1816








    • 1




      Stick a bg in there and win!
      – Joshua
      5 hours ago






    • 1




      @Joshua okay, what do I win now :P
      – The Guy with The Hat
      5 hours ago














    • 1




      Stick a bg in there and win!
      – Joshua
      5 hours ago






    • 1




      @Joshua okay, what do I win now :P
      – The Guy with The Hat
      5 hours ago








    1




    1




    Stick a bg in there and win!
    – Joshua
    5 hours ago




    Stick a bg in there and win!
    – Joshua
    5 hours ago




    1




    1




    @Joshua okay, what do I win now :P
    – The Guy with The Hat
    5 hours ago




    @Joshua okay, what do I win now :P
    – The Guy with The Hat
    5 hours ago










    up vote
    2
    down vote













    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



























      up vote
      2
      down vote













      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

























        up vote
        2
        down vote










        up vote
        2
        down vote









        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 2 hours ago

























        answered 15 hours ago









        sudodus

        73616




        73616






















            up vote
            1
            down vote













            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
              15 hours ago










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










            • I'd like the second command to be started when the first command finishes
              – czerny
              14 hours ago






            • 1




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















            up vote
            1
            down vote













            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
              15 hours ago










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










            • I'd like the second command to be started when the first command finishes
              – czerny
              14 hours ago






            • 1




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













            up vote
            1
            down vote










            up vote
            1
            down vote









            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 15 hours ago









            Tomasz

            9,17052964




            9,17052964












            • 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
              15 hours ago










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










            • I'd like the second command to be started when the first command finishes
              – czerny
              14 hours ago






            • 1




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


















            • 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
              15 hours ago










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










            • I'd like the second command to be started when the first command finishes
              – czerny
              14 hours ago






            • 1




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
















            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
            15 hours ago




            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
            15 hours ago












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




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












            I'd like the second command to be started when the first command finishes
            – czerny
            14 hours ago




            I'd like the second command to be started when the first command finishes
            – czerny
            14 hours ago




            1




            1




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




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










            up vote
            -2
            down vote













            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








            New contributor




            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.














            • 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
              14 hours ago










            • @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
              13 hours ago















            up vote
            -2
            down vote













            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








            New contributor




            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.














            • 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
              14 hours ago










            • @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
              13 hours ago













            up vote
            -2
            down vote










            up vote
            -2
            down vote









            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








            New contributor




            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            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








            New contributor




            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 15 hours ago









            Prasanna

            1012




            1012




            New contributor




            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Prasanna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.








            • 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
              14 hours ago










            • @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
              13 hours ago














            • 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
              14 hours ago










            • @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
              13 hours ago








            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
            14 hours ago




            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
            14 hours ago












            @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
            13 hours ago




            @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
            13 hours ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487955%2fhow-to-plan-a-task-to-run-after-another-already-running-task-in-bash%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Котор

            Потомский, Вадим Владимирович

            Бедствия войны