by mindwok on 10/5/22, 1:19 AM
I was curious how this worked so I did some reading which I found interesting. In short, this uses ptrace, which is the same Linux kernel mechanism used by debuggers, to attach to a running process and manipulate the open file descriptors (amongst other things) while it runs. So basically it attaches to the process, and changes stdin (fd 0) and stdout (fd 1) to point to your tmux session stdout and stdin.
Pretty cool use of ptrace.
by kennu on 10/4/22, 10:11 PM
It's a nice tool (reptyr), but not particularly convenient to use, requiring several steps to move the process to the background and remembering the PID etc. I do wish this was a more streamlined, standard feature of Unix/Linux shells. Something like a simple integrated "pushproc"/"popproc" command.
by outworlder on 10/5/22, 12:05 AM
> Take away the ownership from the shell using disown
Interesting. So far, my only ever use of disown was to decouple GUI apps that I launched from the terminal. I do that automatically now, as it's annoying when you forget, close the terminal, and a bunch of apps go away.
by jaimehrubiks on 10/4/22, 11:06 PM
As others said, wish this use case had been considered on Unix based OSs. I guess it's a very common issue many people have daily. I don't like to do it but some people have some bashrc or ssh config script to auto attach every session to a tmux session or even docker container.
by _kst_ on 10/4/22, 11:35 PM
I tried this on Ubuntu 22.04.1 and got an error:
$ reptyr 10330
Unable to attach to pid 10330: Operation not permitted
The kernel denied permission while attaching. If your uid matches
the target's, check the value of /proc/sys/kernel/yama/ptrace_scope.
For more information, see /etc/sysctl.d/10-ptrace.conf
$
I haven't (yet) looked into how to fix or work around this problem.
by leni536 on 10/5/22, 6:26 AM
> It’s rare, but sometimes it still happens that I forget to open a tmux or screen session when working with something that is supposed to be quickly done.
There are various ways to get ahead of this problem too if you ssh into a tmux session automatically. There are various ways to achieve this.
I didn't know about reptyr, it's very cool. There were times I wished this was possible, but whenever I searched for a solution I got "It's not possible". Oh well...
by amelius on 10/4/22, 10:59 PM
One problem of course is that you miss the output of the process from before you took over control. Perhaps shells could be extended to better support this way of using tmux/screen.
By the way another approach is of course to use VNC (e.g. some process started in an xterm at work you can access by opening your entire desktop at home.)
by 1MachineElf on 10/5/22, 4:46 AM
I feel embarrassed to call myself a tmux fan learning this has been around since at least 2011.
by CoolCold on 10/5/22, 11:42 AM
not denying value of the article, it's great, but kinda solved problem for me - for multiple years, 99% of cases the first command after ssh login to server is `tmux attach || tmux` for me. Almost never run anything not in tmux/screen.
by ivanb on 10/5/22, 10:56 AM
The ability to attach a detached process to any terminal should be a coreutil.
by tpoacher on 10/5/22, 5:48 PM
Unrelated to the article, but my favourite tmux trick is joining an existing tmux session to use as a collaborative (terminal) environment over ssh.
Remote pair coding made fun again!
by johnklos on 10/4/22, 9:55 PM
Very cool! I hope this becomes a standard feature of all common Unix OSes. Will have to look at what part of this is architecture specific so it can be properly abstracted.
by green-salt on 10/5/22, 3:07 AM
I can think of several instances in the past where I wished I could have done this. Learning about screen back in 2007 was life changing.
by meatjuice on 10/5/22, 4:57 AM
The problem is, however, it works ONLY on Linux.
by vasvir on 10/5/22, 7:14 AM
Very cool. Thanks for posting this. I tried with screen and I can confirm that it also works.
by mateusfreira on 10/5/22, 10:06 AM
Amazing short post ... Yesterday I needed this, what a coincidence!!