Putting a foreground process into the background from a background process












-1















Is it possible to put a process into the background from another background process with bash?



For instance, if we have the following code:



function check_if_server_running {
while [ server_running -ne true ]
curl localhost:8000
done
bg %-
}

check_if_server_running &
./startServer

# Some other code...


Could we replace bg %- with something to make the ./startServer run in the background once it's up? Normally bg %- puts the last process in the background but it is not working for me










share|improve this question





























    -1















    Is it possible to put a process into the background from another background process with bash?



    For instance, if we have the following code:



    function check_if_server_running {
    while [ server_running -ne true ]
    curl localhost:8000
    done
    bg %-
    }

    check_if_server_running &
    ./startServer

    # Some other code...


    Could we replace bg %- with something to make the ./startServer run in the background once it's up? Normally bg %- puts the last process in the background but it is not working for me










    share|improve this question



























      -1












      -1








      -1








      Is it possible to put a process into the background from another background process with bash?



      For instance, if we have the following code:



      function check_if_server_running {
      while [ server_running -ne true ]
      curl localhost:8000
      done
      bg %-
      }

      check_if_server_running &
      ./startServer

      # Some other code...


      Could we replace bg %- with something to make the ./startServer run in the background once it's up? Normally bg %- puts the last process in the background but it is not working for me










      share|improve this question
















      Is it possible to put a process into the background from another background process with bash?



      For instance, if we have the following code:



      function check_if_server_running {
      while [ server_running -ne true ]
      curl localhost:8000
      done
      bg %-
      }

      check_if_server_running &
      ./startServer

      # Some other code...


      Could we replace bg %- with something to make the ./startServer run in the background once it's up? Normally bg %- puts the last process in the background but it is not working for me







      bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 9 at 0:39









      Rui F Ribeiro

      40.4k1479137




      40.4k1479137










      asked Oct 16 '18 at 0:56









      User_KSUser_KS

      12




      12






















          1 Answer
          1






          active

          oldest

          votes


















          1














          No, that doesn't really work this way, since the bg and fg commands are only available in the interactive mode of the shell, which is definitely not the case when you're running a function or subshell in background already...



          (Also, bg itself doesn't put a job that is already in background, it just resumes a stopped job in background, you need to stop the job first with Ctrl+Z.)



          Consider reworking your code to start the server in background, then wait until it's running in foreground:



          function wait_for_server_running {
          while ! server_running; do
          curl localhost:8000
          done
          }

          ./startServer &
          wait_for_server_running

          # Some other code...


          That looks similar to what you were trying to achieve, right?






          share|improve this answer
























          • Yes I was trying to do that before, but several unrelated things where blocking me from achieving that. I'll try working around that so I can start the server like that

            – User_KS
            Oct 16 '18 at 16:43











          • @User_KS Feel free to post questions about the issues you're having with the other approach, it should be easy for you to get help with that here, since it's a fairly standard approach to it. Cheers!

            – filbranden
            Oct 16 '18 at 17:03











          • thank you. I've been having this weird issue where my server does not run when it is run from the background. Theres some weird behaviour when I try to run it from the background, but usually it starts the server and immediately exits it. I'm using Play framework, which uses NettyServer for its server implementation, but it still doesn't work. I've done digging for days now and the most likely answer I could find was that theres a SIGTTIN or SIGTTOU signal being passed

            – User_KS
            Oct 16 '18 at 17:08











          • Other people have had a similar problem before: stackoverflow.com/questions/40643208/how-to-run-sbt-as-daemon

            – User_KS
            Oct 16 '18 at 17:08











          • And since Play uses TypeSafe Activator to run, which is based on SBT, that seems to be the most likely culprit. Don't know why it isn't working though

            – User_KS
            Oct 16 '18 at 17:09











          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%2f475706%2fputting-a-foreground-process-into-the-background-from-a-background-process%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          No, that doesn't really work this way, since the bg and fg commands are only available in the interactive mode of the shell, which is definitely not the case when you're running a function or subshell in background already...



          (Also, bg itself doesn't put a job that is already in background, it just resumes a stopped job in background, you need to stop the job first with Ctrl+Z.)



          Consider reworking your code to start the server in background, then wait until it's running in foreground:



          function wait_for_server_running {
          while ! server_running; do
          curl localhost:8000
          done
          }

          ./startServer &
          wait_for_server_running

          # Some other code...


          That looks similar to what you were trying to achieve, right?






          share|improve this answer
























          • Yes I was trying to do that before, but several unrelated things where blocking me from achieving that. I'll try working around that so I can start the server like that

            – User_KS
            Oct 16 '18 at 16:43











          • @User_KS Feel free to post questions about the issues you're having with the other approach, it should be easy for you to get help with that here, since it's a fairly standard approach to it. Cheers!

            – filbranden
            Oct 16 '18 at 17:03











          • thank you. I've been having this weird issue where my server does not run when it is run from the background. Theres some weird behaviour when I try to run it from the background, but usually it starts the server and immediately exits it. I'm using Play framework, which uses NettyServer for its server implementation, but it still doesn't work. I've done digging for days now and the most likely answer I could find was that theres a SIGTTIN or SIGTTOU signal being passed

            – User_KS
            Oct 16 '18 at 17:08











          • Other people have had a similar problem before: stackoverflow.com/questions/40643208/how-to-run-sbt-as-daemon

            – User_KS
            Oct 16 '18 at 17:08











          • And since Play uses TypeSafe Activator to run, which is based on SBT, that seems to be the most likely culprit. Don't know why it isn't working though

            – User_KS
            Oct 16 '18 at 17:09
















          1














          No, that doesn't really work this way, since the bg and fg commands are only available in the interactive mode of the shell, which is definitely not the case when you're running a function or subshell in background already...



          (Also, bg itself doesn't put a job that is already in background, it just resumes a stopped job in background, you need to stop the job first with Ctrl+Z.)



          Consider reworking your code to start the server in background, then wait until it's running in foreground:



          function wait_for_server_running {
          while ! server_running; do
          curl localhost:8000
          done
          }

          ./startServer &
          wait_for_server_running

          # Some other code...


          That looks similar to what you were trying to achieve, right?






          share|improve this answer
























          • Yes I was trying to do that before, but several unrelated things where blocking me from achieving that. I'll try working around that so I can start the server like that

            – User_KS
            Oct 16 '18 at 16:43











          • @User_KS Feel free to post questions about the issues you're having with the other approach, it should be easy for you to get help with that here, since it's a fairly standard approach to it. Cheers!

            – filbranden
            Oct 16 '18 at 17:03











          • thank you. I've been having this weird issue where my server does not run when it is run from the background. Theres some weird behaviour when I try to run it from the background, but usually it starts the server and immediately exits it. I'm using Play framework, which uses NettyServer for its server implementation, but it still doesn't work. I've done digging for days now and the most likely answer I could find was that theres a SIGTTIN or SIGTTOU signal being passed

            – User_KS
            Oct 16 '18 at 17:08











          • Other people have had a similar problem before: stackoverflow.com/questions/40643208/how-to-run-sbt-as-daemon

            – User_KS
            Oct 16 '18 at 17:08











          • And since Play uses TypeSafe Activator to run, which is based on SBT, that seems to be the most likely culprit. Don't know why it isn't working though

            – User_KS
            Oct 16 '18 at 17:09














          1












          1








          1







          No, that doesn't really work this way, since the bg and fg commands are only available in the interactive mode of the shell, which is definitely not the case when you're running a function or subshell in background already...



          (Also, bg itself doesn't put a job that is already in background, it just resumes a stopped job in background, you need to stop the job first with Ctrl+Z.)



          Consider reworking your code to start the server in background, then wait until it's running in foreground:



          function wait_for_server_running {
          while ! server_running; do
          curl localhost:8000
          done
          }

          ./startServer &
          wait_for_server_running

          # Some other code...


          That looks similar to what you were trying to achieve, right?






          share|improve this answer













          No, that doesn't really work this way, since the bg and fg commands are only available in the interactive mode of the shell, which is definitely not the case when you're running a function or subshell in background already...



          (Also, bg itself doesn't put a job that is already in background, it just resumes a stopped job in background, you need to stop the job first with Ctrl+Z.)



          Consider reworking your code to start the server in background, then wait until it's running in foreground:



          function wait_for_server_running {
          while ! server_running; do
          curl localhost:8000
          done
          }

          ./startServer &
          wait_for_server_running

          # Some other code...


          That looks similar to what you were trying to achieve, right?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 16 '18 at 4:41









          filbrandenfilbranden

          8,71621241




          8,71621241













          • Yes I was trying to do that before, but several unrelated things where blocking me from achieving that. I'll try working around that so I can start the server like that

            – User_KS
            Oct 16 '18 at 16:43











          • @User_KS Feel free to post questions about the issues you're having with the other approach, it should be easy for you to get help with that here, since it's a fairly standard approach to it. Cheers!

            – filbranden
            Oct 16 '18 at 17:03











          • thank you. I've been having this weird issue where my server does not run when it is run from the background. Theres some weird behaviour when I try to run it from the background, but usually it starts the server and immediately exits it. I'm using Play framework, which uses NettyServer for its server implementation, but it still doesn't work. I've done digging for days now and the most likely answer I could find was that theres a SIGTTIN or SIGTTOU signal being passed

            – User_KS
            Oct 16 '18 at 17:08











          • Other people have had a similar problem before: stackoverflow.com/questions/40643208/how-to-run-sbt-as-daemon

            – User_KS
            Oct 16 '18 at 17:08











          • And since Play uses TypeSafe Activator to run, which is based on SBT, that seems to be the most likely culprit. Don't know why it isn't working though

            – User_KS
            Oct 16 '18 at 17:09



















          • Yes I was trying to do that before, but several unrelated things where blocking me from achieving that. I'll try working around that so I can start the server like that

            – User_KS
            Oct 16 '18 at 16:43











          • @User_KS Feel free to post questions about the issues you're having with the other approach, it should be easy for you to get help with that here, since it's a fairly standard approach to it. Cheers!

            – filbranden
            Oct 16 '18 at 17:03











          • thank you. I've been having this weird issue where my server does not run when it is run from the background. Theres some weird behaviour when I try to run it from the background, but usually it starts the server and immediately exits it. I'm using Play framework, which uses NettyServer for its server implementation, but it still doesn't work. I've done digging for days now and the most likely answer I could find was that theres a SIGTTIN or SIGTTOU signal being passed

            – User_KS
            Oct 16 '18 at 17:08











          • Other people have had a similar problem before: stackoverflow.com/questions/40643208/how-to-run-sbt-as-daemon

            – User_KS
            Oct 16 '18 at 17:08











          • And since Play uses TypeSafe Activator to run, which is based on SBT, that seems to be the most likely culprit. Don't know why it isn't working though

            – User_KS
            Oct 16 '18 at 17:09

















          Yes I was trying to do that before, but several unrelated things where blocking me from achieving that. I'll try working around that so I can start the server like that

          – User_KS
          Oct 16 '18 at 16:43





          Yes I was trying to do that before, but several unrelated things where blocking me from achieving that. I'll try working around that so I can start the server like that

          – User_KS
          Oct 16 '18 at 16:43













          @User_KS Feel free to post questions about the issues you're having with the other approach, it should be easy for you to get help with that here, since it's a fairly standard approach to it. Cheers!

          – filbranden
          Oct 16 '18 at 17:03





          @User_KS Feel free to post questions about the issues you're having with the other approach, it should be easy for you to get help with that here, since it's a fairly standard approach to it. Cheers!

          – filbranden
          Oct 16 '18 at 17:03













          thank you. I've been having this weird issue where my server does not run when it is run from the background. Theres some weird behaviour when I try to run it from the background, but usually it starts the server and immediately exits it. I'm using Play framework, which uses NettyServer for its server implementation, but it still doesn't work. I've done digging for days now and the most likely answer I could find was that theres a SIGTTIN or SIGTTOU signal being passed

          – User_KS
          Oct 16 '18 at 17:08





          thank you. I've been having this weird issue where my server does not run when it is run from the background. Theres some weird behaviour when I try to run it from the background, but usually it starts the server and immediately exits it. I'm using Play framework, which uses NettyServer for its server implementation, but it still doesn't work. I've done digging for days now and the most likely answer I could find was that theres a SIGTTIN or SIGTTOU signal being passed

          – User_KS
          Oct 16 '18 at 17:08













          Other people have had a similar problem before: stackoverflow.com/questions/40643208/how-to-run-sbt-as-daemon

          – User_KS
          Oct 16 '18 at 17:08





          Other people have had a similar problem before: stackoverflow.com/questions/40643208/how-to-run-sbt-as-daemon

          – User_KS
          Oct 16 '18 at 17:08













          And since Play uses TypeSafe Activator to run, which is based on SBT, that seems to be the most likely culprit. Don't know why it isn't working though

          – User_KS
          Oct 16 '18 at 17:09





          And since Play uses TypeSafe Activator to run, which is based on SBT, that seems to be the most likely culprit. Don't know why it isn't working though

          – User_KS
          Oct 16 '18 at 17:09


















          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%2f475706%2fputting-a-foreground-process-into-the-background-from-a-background-process%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世紀