How to move job into background after already started

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    How to move job into background after already started

    i know to use nohup, but what if the user started a very long job and wanted to leave (logoff/logout) without killing the job.

    Is there away to move an existing process into the background without restarting it?

    This is a situation that came up by a colleague, but curious to know the answer for my future reference.


    Thanks guys,


    Dan
  • AmberJain
    Recognized Expert Contributor
    • Jan 2008
    • 922

    #2
    You can either nohup or detach a process. You can try using some terminal multiplexer (google tmux or screen) and then detaching the terminal before you logout.

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Also, if you are running a process from the command line and the command line is tied up by the process and you need it back, you can do a "ctrl-Z" and then type bg.

      That will background the process you have running.

      Regards,

      Jeff

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        Originally posted by numberwhun
        Also, if you are running a process from the command line and the command line is tied up by the process and you need it back, you can do a "ctrl-Z" and then type bg.

        That will background the process you have running.

        Regards,

        Jeff
        But putting in in background then logging out, still keeps it going?

        @Amber, ya, if it was a screen session, I could just detach it. Personally, I always use screen from my gateway host, but if I'm ever on some unusual host that away from our network doesn't have screen, I wouldn't know how to do it.

        i'm going to test the bg method and log out, see if the job is still running. Will post back what I find.


        Dan

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Worked!

          Thanks @numberwhun


          Dan

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Unfortunately, no, if you log out it probably won't keep going. Its not nohup. But, it does allow you to get our command prompt back.

            Also, if you have a program that you know is going to hijack your prompt, then put a & at the end of the line like so:

            Code:
            ./command -opt1 -opt2 &
            That will automatically background the job and give you your prompt back immediately.

            Regards,

            Jeff

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              that's weird, here's what I did. A php script that in a for loop writes a number to a text file every few seconds. I ran it, Ctrl+Z, then bg and logged off. (when it was at 10)

              logged back in about half a minute later and tail -f 'd the file and it was in the 20's or something. It was obviously running when I was offline.

              I'll reconfirm and log off for a greater period or log in from another user's and follow the text file.

              I'm pretty sure it kept running.


              Dan

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                That is weird because ctrl-z is an interrupt.

                Comment

                • ashitpro
                  Recognized Expert Contributor
                  • Aug 2007
                  • 542

                  #9
                  try 'setsid' command...
                  It will create a process which will run your job and its parent will be init.
                  I am not sure about behavior after logoff.
                  But theoretically it should work as we are assigning it to init.

                  Comment

                  Working...