Launch a background process and check when it ends












29















How can I launch a process in background and check when it ends within a bash script?
My idea is a script like this:



launch backgroundprocess &
while [ Process is running ];do
echo "PROCESS IS RUNNINGr"
done;

echo "PROCESS TERMINATED"









share|improve this question















migrated from serverfault.com May 22 '13 at 16:53


This question came from our site for system and network administrators.























    29















    How can I launch a process in background and check when it ends within a bash script?
    My idea is a script like this:



    launch backgroundprocess &
    while [ Process is running ];do
    echo "PROCESS IS RUNNINGr"
    done;

    echo "PROCESS TERMINATED"









    share|improve this question















    migrated from serverfault.com May 22 '13 at 16:53


    This question came from our site for system and network administrators.





















      29












      29








      29


      16






      How can I launch a process in background and check when it ends within a bash script?
      My idea is a script like this:



      launch backgroundprocess &
      while [ Process is running ];do
      echo "PROCESS IS RUNNINGr"
      done;

      echo "PROCESS TERMINATED"









      share|improve this question
















      How can I launch a process in background and check when it ends within a bash script?
      My idea is a script like this:



      launch backgroundprocess &
      while [ Process is running ];do
      echo "PROCESS IS RUNNINGr"
      done;

      echo "PROCESS TERMINATED"






      bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 28 at 20:35









      codeforester

      378317




      378317










      asked May 22 '13 at 12:34









      0wn3r0wn3r

      246135




      246135




      migrated from serverfault.com May 22 '13 at 16:53


      This question came from our site for system and network administrators.









      migrated from serverfault.com May 22 '13 at 16:53


      This question came from our site for system and network administrators.
























          4 Answers
          4






          active

          oldest

          votes


















          57














          The key is the "wait" command:



          #!/bin/bash

          /my/process &
          /another/process &
          wait
          echo "All processes done!"





          share|improve this answer



















          • 6





            Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;)

            – Pascal Schmiel
            May 22 '13 at 12:47






          • 1





            The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

            – Mark Booth
            Apr 28 '15 at 10:30






          • 1





            @Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

            – William Pursell
            Aug 2 '17 at 17:26



















          27














          With wait you can have the granularity you need:



          sleep 1 &
          PID1=$!
          sleep 2 &
          PID2=$!

          wait $PID1
          echo 'PID1 has ended.'
          wait
          echo 'All background processes have exited.'





          share|improve this answer

































            25














            Here is one way to do it:



            launch backgroundprocess &
            PROC_ID=$!

            while kill -0 "$PROC_ID" >/dev/null 2>&1; do
            echo "PROCESS IS RUNNING"
            done
            echo "PROCESS TERMINATED"
            exit 0





            share|improve this answer





















            • 2





              Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

              – Mark Booth
              Apr 30 '15 at 14:48











            • @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?

              – Nishant
              Dec 5 '18 at 9:30



















            0














            You can run your process with nohup and write shell script to read nohup.out file which nohup uses to log .



              nohup command &





            share|improve this answer



















            • 3





              (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output?

              – G-Man
              Oct 23 '15 at 13:29











            • @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method.

              – Alexej Magura
              Sep 15 '17 at 16:39













            • (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running?

              – G-Man
              Sep 16 '17 at 1:38











            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',
            autoActivateHeartbeat: false,
            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%2f76717%2flaunch-a-background-process-and-check-when-it-ends%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            57














            The key is the "wait" command:



            #!/bin/bash

            /my/process &
            /another/process &
            wait
            echo "All processes done!"





            share|improve this answer



















            • 6





              Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;)

              – Pascal Schmiel
              May 22 '13 at 12:47






            • 1





              The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

              – Mark Booth
              Apr 28 '15 at 10:30






            • 1





              @Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

              – William Pursell
              Aug 2 '17 at 17:26
















            57














            The key is the "wait" command:



            #!/bin/bash

            /my/process &
            /another/process &
            wait
            echo "All processes done!"





            share|improve this answer



















            • 6





              Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;)

              – Pascal Schmiel
              May 22 '13 at 12:47






            • 1





              The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

              – Mark Booth
              Apr 28 '15 at 10:30






            • 1





              @Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

              – William Pursell
              Aug 2 '17 at 17:26














            57












            57








            57







            The key is the "wait" command:



            #!/bin/bash

            /my/process &
            /another/process &
            wait
            echo "All processes done!"





            share|improve this answer













            The key is the "wait" command:



            #!/bin/bash

            /my/process &
            /another/process &
            wait
            echo "All processes done!"






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 22 '13 at 12:39









            Pascal SchmielPascal Schmiel

            82965




            82965








            • 6





              Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;)

              – Pascal Schmiel
              May 22 '13 at 12:47






            • 1





              The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

              – Mark Booth
              Apr 28 '15 at 10:30






            • 1





              @Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

              – William Pursell
              Aug 2 '17 at 17:26














            • 6





              Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;)

              – Pascal Schmiel
              May 22 '13 at 12:47






            • 1





              The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

              – Mark Booth
              Apr 28 '15 at 10:30






            • 1





              @Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

              – William Pursell
              Aug 2 '17 at 17:26








            6




            6





            Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;)

            – Pascal Schmiel
            May 22 '13 at 12:47





            Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;)

            – Pascal Schmiel
            May 22 '13 at 12:47




            1




            1





            The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

            – Mark Booth
            Apr 28 '15 at 10:30





            The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

            – Mark Booth
            Apr 28 '15 at 10:30




            1




            1





            @Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

            – William Pursell
            Aug 2 '17 at 17:26





            @Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

            – William Pursell
            Aug 2 '17 at 17:26













            27














            With wait you can have the granularity you need:



            sleep 1 &
            PID1=$!
            sleep 2 &
            PID2=$!

            wait $PID1
            echo 'PID1 has ended.'
            wait
            echo 'All background processes have exited.'





            share|improve this answer






























              27














              With wait you can have the granularity you need:



              sleep 1 &
              PID1=$!
              sleep 2 &
              PID2=$!

              wait $PID1
              echo 'PID1 has ended.'
              wait
              echo 'All background processes have exited.'





              share|improve this answer




























                27












                27








                27







                With wait you can have the granularity you need:



                sleep 1 &
                PID1=$!
                sleep 2 &
                PID2=$!

                wait $PID1
                echo 'PID1 has ended.'
                wait
                echo 'All background processes have exited.'





                share|improve this answer















                With wait you can have the granularity you need:



                sleep 1 &
                PID1=$!
                sleep 2 &
                PID2=$!

                wait $PID1
                echo 'PID1 has ended.'
                wait
                echo 'All background processes have exited.'






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 6 '16 at 17:50

























                answered May 22 '13 at 13:57









                Mircea VutcoviciMircea Vutcovici

                1,301108




                1,301108























                    25














                    Here is one way to do it:



                    launch backgroundprocess &
                    PROC_ID=$!

                    while kill -0 "$PROC_ID" >/dev/null 2>&1; do
                    echo "PROCESS IS RUNNING"
                    done
                    echo "PROCESS TERMINATED"
                    exit 0





                    share|improve this answer





















                    • 2





                      Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

                      – Mark Booth
                      Apr 30 '15 at 14:48











                    • @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?

                      – Nishant
                      Dec 5 '18 at 9:30
















                    25














                    Here is one way to do it:



                    launch backgroundprocess &
                    PROC_ID=$!

                    while kill -0 "$PROC_ID" >/dev/null 2>&1; do
                    echo "PROCESS IS RUNNING"
                    done
                    echo "PROCESS TERMINATED"
                    exit 0





                    share|improve this answer





















                    • 2





                      Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

                      – Mark Booth
                      Apr 30 '15 at 14:48











                    • @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?

                      – Nishant
                      Dec 5 '18 at 9:30














                    25












                    25








                    25







                    Here is one way to do it:



                    launch backgroundprocess &
                    PROC_ID=$!

                    while kill -0 "$PROC_ID" >/dev/null 2>&1; do
                    echo "PROCESS IS RUNNING"
                    done
                    echo "PROCESS TERMINATED"
                    exit 0





                    share|improve this answer















                    Here is one way to do it:



                    launch backgroundprocess &
                    PROC_ID=$!

                    while kill -0 "$PROC_ID" >/dev/null 2>&1; do
                    echo "PROCESS IS RUNNING"
                    done
                    echo "PROCESS TERMINATED"
                    exit 0






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 23 '15 at 13:23









                    Community

                    1




                    1










                    answered May 22 '13 at 12:42









                    cuonglmcuonglm

                    105k25209307




                    105k25209307








                    • 2





                      Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

                      – Mark Booth
                      Apr 30 '15 at 14:48











                    • @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?

                      – Nishant
                      Dec 5 '18 at 9:30














                    • 2





                      Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

                      – Mark Booth
                      Apr 30 '15 at 14:48











                    • @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?

                      – Nishant
                      Dec 5 '18 at 9:30








                    2




                    2





                    Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

                    – Mark Booth
                    Apr 30 '15 at 14:48





                    Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

                    – Mark Booth
                    Apr 30 '15 at 14:48













                    @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?

                    – Nishant
                    Dec 5 '18 at 9:30





                    @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?

                    – Nishant
                    Dec 5 '18 at 9:30











                    0














                    You can run your process with nohup and write shell script to read nohup.out file which nohup uses to log .



                      nohup command &





                    share|improve this answer



















                    • 3





                      (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output?

                      – G-Man
                      Oct 23 '15 at 13:29











                    • @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method.

                      – Alexej Magura
                      Sep 15 '17 at 16:39













                    • (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running?

                      – G-Man
                      Sep 16 '17 at 1:38
















                    0














                    You can run your process with nohup and write shell script to read nohup.out file which nohup uses to log .



                      nohup command &





                    share|improve this answer



















                    • 3





                      (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output?

                      – G-Man
                      Oct 23 '15 at 13:29











                    • @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method.

                      – Alexej Magura
                      Sep 15 '17 at 16:39













                    • (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running?

                      – G-Man
                      Sep 16 '17 at 1:38














                    0












                    0








                    0







                    You can run your process with nohup and write shell script to read nohup.out file which nohup uses to log .



                      nohup command &





                    share|improve this answer













                    You can run your process with nohup and write shell script to read nohup.out file which nohup uses to log .



                      nohup command &






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 22 '13 at 12:37









                    Abhishek Anand AmralkarAbhishek Anand Amralkar

                    1112




                    1112








                    • 3





                      (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output?

                      – G-Man
                      Oct 23 '15 at 13:29











                    • @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method.

                      – Alexej Magura
                      Sep 15 '17 at 16:39













                    • (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running?

                      – G-Man
                      Sep 16 '17 at 1:38














                    • 3





                      (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output?

                      – G-Man
                      Oct 23 '15 at 13:29











                    • @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method.

                      – Alexej Magura
                      Sep 15 '17 at 16:39













                    • (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running?

                      – G-Man
                      Sep 16 '17 at 1:38








                    3




                    3





                    (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output?

                    – G-Man
                    Oct 23 '15 at 13:29





                    (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output?

                    – G-Man
                    Oct 23 '15 at 13:29













                    @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method.

                    – Alexej Magura
                    Sep 15 '17 at 16:39







                    @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method.

                    – Alexej Magura
                    Sep 15 '17 at 16:39















                    (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running?

                    – G-Man
                    Sep 16 '17 at 1:38





                    (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running?

                    – G-Man
                    Sep 16 '17 at 1:38


















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f76717%2flaunch-a-background-process-and-check-when-it-ends%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

                    How to make a Squid Proxy server?

                    Is this a new Fibonacci Identity?

                    19世紀