How can I disown a running process and associate it to a new screen shell?

142,265

Solution 1

You can revoke “ownership” of the program from the shell with the disown built-in:

# press Ctrl+Z to suspend the program
bg
disown

However this only tells the shell not to send a SIGHUP signal to the program when the shell exits. The program will retain any connection it has with the terminal, usually as standard input, output and error streams. There is no way to reattach those to another terminal. (Screen works by emulating a terminal for each window, so the programs are attached to the screen window.)


It is possible to reattach the filedescriptors to a different file by attaching the program in a debugger (i.e. using ptrace) and making it call open, dup and close. There are a few tools that do this; this is a tricky process, and sometimes they will crash the process instead. The possibilities include (links collected from answers to How can I disown a running process and associate it to a new screen shell? and Can I nohup/screen an already-started process?):

Solution 2

Using GNU screen is your best bet.

Start screen running when you first login - I run screen -D -R, run your command, and either disconnect or suspend it with CTRL-Z and then disconnect from screen by pressing CTRL-A then D.

When you login to the machine again, reconnect by running screen -D -R. You will be in the same shell as before. You can run jobs to see the suspended process if you did so, and run %1 (or the respective job #) to foreground it again.

Solution 3

To move a process between terminals or to reattach a disowned, you can use e.g. reptyr.

Solution 4

My favorite solution is using tmux, you could detach the session, and re-attach it in another terminal.

When you detached from previous session, you can safely close the terminal; later use tmux attach to get back to the session, even if you logged out.

Solution 5

There's also a small utility called retty that lets you reattach running programs to another terminal.

Share:
142,265

Related videos on Youtube

levesque
Author by

levesque

Updated on September 17, 2022

Comments

  • levesque
    levesque over 1 year

    I have a running program on a SSH shell. I want to pause it and be able to unpause its execution when I come back.

    One way I thought of doing that was to transfer its ownership to a screen shell, thus keeping it running in there.

    Is there a different way to proceed?

    • Admin
      Admin over 13 years
      See also Can I nohup/screen an already-started process? and Resume command running in dropped SSH session, which mention several ptrace-based solutions not (currently) mentioned here.
    • Admin
      Admin about 12 years
      From questions like unix.stackexchange.com/a/4039/13496 I'm hearing about retty and neercs. Hmmm... wonder if there's smth like a "screen here" layer before I run a process next time should I lose the top terminal in the future, that will make it easy to snap back in the stdin/out/err
    • Admin
      Admin about 12 years
      The secondary/implicit issue in this question I can't fathom is...why did the shell choose to disown a suspended job when there is a newer/just launched one in background that doesn't even need/wait for stdin? This is the treatment I've gotten used to so don't know what went different here...
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 13 years
    Interesting. It does play dirty (ptrace), but it doesn't just manipulate the file descriptors, it forks the process. It's able to grab find /, but crashed an interactive bash.
  • Michael Mrozek
    Michael Mrozek over 13 years
    @Gilles I can't remember how it went when I tried, but it doesn't have a great reputation, I'm told it fails pretty regularly
  • RIQ
    RIQ about 12 years
    Yeah that saved it, thanks! I read the author's website how it works better than similar or older tools eg. for ncurses programs.
  • Kendall P. Webber
    Kendall P. Webber over 10 years
    you also can share your session with ur friend and use multiple windows and panes and so much more! love it ^_^
  • ctrl-alt-delor
    ctrl-alt-delor about 9 years
    disown removes the process from the job-control list.
  • Владислав Щербин
    Владислав Щербин about 9 years
    I think this doesn't answer the question. The question begins with "I have a program running". This answer assumes it's not running yet…
  • toxefa
    toxefa about 9 years
    +1 Although the accepted screen answer is of course ideal, it doesn't actually answer the question, which specifically requests a way to move a currently running process to screen or the like. Also see this answer: serverfault.com/a/284795
  • Cees Timmerman
    Cees Timmerman almost 9 years
    Why not disown -h?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 9 years
    @CeesTimmerman That leaves the job in the shell's job table, but what's the advantage of that?
  • Peter Cordes
    Peter Cordes over 8 years
    @Gilles: so you can still fg or kill it, and see if it ends on its own.
  • dma_k
    dma_k over 8 years
    What mentioned utilities work with a group of processes (e.g. bzcat a.bz2 | grep text)? Man for reptyr says that it does not support moving the processes with children.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    @dma_k In this example, you'd only need to move grep's output.
  • Vitaly Zdanevich
    Vitaly Zdanevich about 8 years
    example of using please?
  • stroncod
    stroncod about 5 years
    Absolute live saver. Allowed me to re-attach to running apt dist-upgrade that was sitting on waiting for user confirmation.
  • Florian Heigl
    Florian Heigl over 4 years
    yes he clearly wrote he wants to get it into a screen session :-)
  • user3728501
    user3728501 almost 4 years
    This doesn't answer the question, or at least it doesn't answer this question which is linked: unix.stackexchange.com/questions/171250/…
  • Qumber
    Qumber almost 4 years
    Can be installed using APT on Debian based distros: sudo apt install reptyr.
  • PythoNic
    PythoNic over 2 years
    This answer is great, as you need some preparation, if you want to it right, at least the next time