Sunday, August 10, 2014

Linux Terminal: Reptyr attach a running process to a new terminal


If, like me, you work on terminals connected via ssh to remote computer/server you are probably used to tmux and screen and so it’s not a problem if you have to close your session, as you’ll be able to easily re-connect when you need it, but sometimes you could forget about using one of these utility.
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.

Reptyr is a utility for taking an existing running program and attaching it to a new terminal, and is particularly useful for moving a long-running process into a GNU screen session.
reptyr does a more thorough job of transferring programs than many other tools, including the popular “screenify” shell script, because it changes the program’s controlling terminal. This means that actions such as window resizes and interrupts are sent to the process from the new terminal.


The usage of reptyr it’s quiet easy, just discover the PID of the process you want to “move” to screen with a ps command and run the command:
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.)
As a bonus feature, if you run reptyr -l, reptyr will create a new pseudo-terminal pair with nothing attached to the slave end, and print its name out.
If you are debugging a program in gdb, you can pass that name to “set inferior-pty”. Because there is no existing program listening to that tty, this will work much better than passing an existing shell’s terminal.

No comments:

Post a Comment