276

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?

6

3 Answers 3

278

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.)

11
  • 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, 2014 at 9: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. Sep 16, 2016 at 20:00
  • 11
    Doesn't work except in the most trivial cases: Unable to attach. (This most commonly means that 11690 has a suprocesses)
    – user541686
    Aug 13, 2017 at 0:41
  • 8
    At least as root, you can use reptyr -T PID
    – ChristophK
    Dec 14, 2017 at 14:25
  • 3
    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: manpages.ubuntu.com/manpages/xenial/man1/reptyr.1.html Feb 24, 2019 at 22:53
57

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.

5
  • 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, 2013 at 0:36
  • 13
    FYI: after disown-ing the process it can be reptyr-ed inside screen.
    – Adam
    Jun 9, 2014 at 16:26
  • 3
    This killed my running program :(
    – user541686
    Aug 13, 2017 at 0:49
  • 2
    Improving the comment by @Adam (Adam): FYI: after disown-ing the process it can be reptyr-ed (as described in this answer) inside screen.
    – Cadoiz
    Oct 26, 2021 at 12:03
  • Works fine. To test it I did: mkdir /tmp/fifo1. Then used cat /tmp/fifo1 as the command to be detach from tty.
    – Marinos An
    Mar 28, 2023 at 12:45
41

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

$ screen -S my_process
$ retty $(pgrep my_process)
/redraw
6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .