262

My office job routinely sees me connected to a Linux box via VNC.

Sometimes I start a remote job on the console, and realize later that it runs much longer than expected. (Should have started that one under Screen in the first place...)

I don't want to keep my workstation running overnight just to keep the VNC session open; I would like to move that already-running remote job into a Screen session (on the remote box), so I can power down the workstation (and reconnect next morning).

How can this be done, if at all?

Jeff Atwood
  • 12,994
  • 20
  • 74
  • 92
DevSolar
  • 2,796
  • 3
  • 16
  • 11
  • 2
    and it's a dupe, http://serverfault.com/questions/49877/can-you-screen-and-running-programme - seems it can't be done. – Paul Dixon Aug 18 '09 at 11:08
  • http://superuser.com/questions/28568/screen-a-running-process – warren Apr 29 '10 at 21:00
  • Also see [How can I disown a running process and associate it to a new screen shell?](http://unix.stackexchange.com/questions/4034/how-can-i-disown-a-running-process-and-associate-it-to-a-new-screen-shell). – dma_k Oct 18 '15 at 09:21
  • 3
    Possible duplicate of [Can I nohup/screen an already-started process?](http://serverfault.com/questions/24425/can-i-nohup-screen-an-already-started-process) – scai Feb 24 '17 at 13:22
  • Search for screenify script. – Vashu Feb 27 '17 at 07:09
  • Is there a macOS version for this? – filype Nov 08 '21 at 02:01

3 Answers3

265

Have a look at reptyr, which does exactly that. The github page has all the information.

reptyr - A tool for "re-ptying" programs.

reptyr is a utility for taking an existing running program and attaching it to a new terminal. Started a long-running process over ssh, but have to leave and don't want to interrupt it? Just start a screen, use reptyr to grab it, and then kill the ssh session and head on home.

USAGE

reptyr PID

"reptyr PID" will grab the process with id PID and attach it to your current terminal.

After attaching, the process will take input from and write output to the new terminal, including ^C and ^Z. (Unfortunately, if you background it, you will still have to run "bg" or "fg" in the old terminal. This is likely impossible to fix in a reasonable way without patching your shell.)

Jim Davis
  • 103
  • 3
DaveJohnston
  • 2,805
  • 1
  • 16
  • 9
  • 26
    If, like me, you looked at the link above and briefly considered installing the program from source, take heart: it's just an `apt-get` away on Debian systems. – mlissner Oct 30 '14 at 09:31
  • 4
    This is awesome! I'd upvote it 10 times if I could! Also, reptyr is available in RedHat repos as well. Just 'yum install' to get it. – Mark J. Bobak Sep 16 '16 at 20:00
  • 10
    Doesn't work except in the most trivial cases: `Unable to attach. (This most commonly means that 11690 has a suprocesses)` – user541686 Aug 13 '17 at 00:41
  • 4
    At least as root, you can use `reptyr -T PID` – ChristophK Dec 14 '17 at 14:25
  • 2
    Note to Ubuntu users: reptyr depends on the ptrace(2) system call to attach to the remote program. On Ubuntu Maverick and higher, this ability is disabled by default for security reasons. You can enable it temporarily by doing `echo 0 > /proc/sys/kernel/yama/ptrace_scope` as root, or permanently by editing the file `/etc/sysctl.d/10-ptrace.conf`, which also contains more information about this setting. Source: http://manpages.ubuntu.com/manpages/xenial/man1/reptyr.1.html – Akira Yamamoto Feb 24 '19 at 22:53
  • Is there a way to do this with commands running in a pipe? – lol May 07 '20 at 12:34
  • That's such a nice tool, worked flawless for a php console process. Thanks for saving me hours of work :) – John Jul 02 '20 at 01:24
  • How to use this wonderful utility on Raspberry Pi 4 Ubuntu 20.04 LTS? I don't see it via apt-get. – alphaGeek Apr 26 '21 at 12:45
  • Is there a macOS version for this? – filype Nov 08 '21 at 02:01
  • Hah, the process that I want to attach to is yum, which blocks me from installing reptyr until it has finished :) Oh, well I'll now I know, and can have it installed for the next time. – pavon May 23 '22 at 18:25
52

You cannot do this, easily. I'd suggest making it a habit to start screen as the first thing you do after opening a console.

However, for your actual problem, there's another thing you could try: after having launched your job from the terminal, background it by typing ctrl-z and then bg. After that, detach the job from it's parent shell; in bash you'd do disown -h %<jobid>. After that, you can safely close the terminal and the job will continue running.

earl
  • 2,901
  • 23
  • 16
  • 3
    This worked very well for my case. reptyr and retty both failed to grab the process, but all I needed was to be able to close a terminal and leave the process to run. – STW Dec 28 '13 at 00:36
  • 12
    FYI: after _disown_-ing the process it can be _reptyr_-ed inside _screen_. – Adam Jun 09 '14 at 16:26
  • 3
    This killed my running program :( – user541686 Aug 13 '17 at 00:49
  • 1
    Improving [the comment by](https://serverfault.com/questions/55880/moving-an-already-running-process-to-screen/55882#comment715009_55881) @Adam ([Adam](https://serverfault.com/users/219726/adam)): FYI: after `disown`-ing the process it can be `reptyr`-ed (as described in [this answer](https://serverfault.com/a/284795/515968)) inside `screen`. – Cadoiz Oct 26 '21 at 12:03
40

As you're using Linux, what about using retty inside the newly launched screen process?

$ screen -S my_process
$ retty $(pgrep my_process)
/redraw
Cadoiz
  • 135
  • 5
Rob Wells
  • 604
  • 5
  • 7
  • 2
    Didn't know of retty, thanks. However, for the kind of long-running jobs I have in mind, I'm a bit reluctant to use a tool that refers to itself as "very lightly tested". I go with earl's solution. –  Aug 18 '09 at 13:08
  • 2
    see no retty in Gentoo – Sergei Krivonos Jun 16 '15 at 08:48
  • 1
    cannot attach to 4858 – Arnold Roa Jan 15 '16 at 20:23
  • no retty in debian11 – piotao Sep 21 '21 at 20:46
  • 1
    @piotao [`reptyr` is `apt-get`able for debian](https://serverfault.com/a/284795/515968) and [`yum install`able on RedHat Repos (CentOS,...)](https://serverfault.com/questions/55880/moving-an-already-running-process-to-screen/55882#comment1019204_284795) – Cadoiz Oct 26 '21 at 12:07
  • @arnold Maybe [similar to this](https://serverfault.com/questions/55880/moving-an-already-running-process-to-screen/55882#comment1116643_284795)? – Cadoiz Oct 26 '21 at 12:08