TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to move a running process into a tmux session (2020)

307 pointsby s0l1dsnak3123over 2 years ago

16 comments

mindwokover 2 years ago
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.<p>Pretty cool use of ptrace.
评论 #33090081 未加载
评论 #33094532 未加载
kennuover 2 years ago
It&#x27;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&#x2F;Linux shells. Something like a simple integrated &quot;pushproc&quot;&#x2F;&quot;popproc&quot; command.
评论 #33091555 未加载
评论 #33091780 未加载
评论 #33093770 未加载
outworlderover 2 years ago
&gt; Take away the ownership from the shell using disown<p>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&#x27;s annoying when you forget, close the terminal, and a bunch of apps go away.
评论 #33092985 未加载
评论 #33090943 未加载
评论 #33093679 未加载
jaimehrubiksover 2 years ago
As others said, wish this use case had been considered on Unix based OSs. I guess it&#x27;s a very common issue many people have daily. I don&#x27;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.
评论 #33095454 未加载
评论 #33089212 未加载
_kst_over 2 years ago
I tried this on Ubuntu 22.04.1 and got an error:<p><pre><code> $ reptyr 10330 Unable to attach to pid 10330: Operation not permitted The kernel denied permission while attaching. If your uid matches the target&#x27;s, check the value of &#x2F;proc&#x2F;sys&#x2F;kernel&#x2F;yama&#x2F;ptrace_scope. For more information, see &#x2F;etc&#x2F;sysctl.d&#x2F;10-ptrace.conf $ </code></pre> I haven&#x27;t (yet) looked into how to fix or work around this problem.
评论 #33090060 未加载
评论 #33089762 未加载
leni536over 2 years ago
&gt; 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.<p>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.<p>I didn&#x27;t know about reptyr, it&#x27;s very cool. There were times I wished this was possible, but whenever I searched for a solution I got &quot;It&#x27;s not possible&quot;. Oh well...
评论 #33092436 未加载
评论 #33092705 未加载
ameliusover 2 years ago
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&#x2F;screen.<p>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.)
评论 #33088403 未加载
评论 #33090701 未加载
评论 #33089139 未加载
1MachineElfover 2 years ago
I feel embarrassed to call myself a tmux fan learning this has been around since at least 2011.
CoolColdover 2 years ago
not denying value of the article, it&#x27;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&#x2F;screen.
评论 #33095360 未加载
ivanbover 2 years ago
The ability to attach a detached process to any terminal should be a coreutil.
评论 #33094892 未加载
tpoacherover 2 years ago
Unrelated to the article, but my favourite tmux trick is joining an existing tmux session to use as a collaborative (terminal) environment over ssh.<p>Remote pair coding made fun again!
johnklosover 2 years ago
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.
green-saltover 2 years ago
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.
meatjuiceover 2 years ago
The problem is, however, it works ONLY on Linux.
评论 #33092137 未加载
vasvirover 2 years ago
Very cool. Thanks for posting this. I tried with screen and I can confirm that it also works.
mateusfreiraover 2 years ago
Amazing short post ... Yesterday I needed this, what a coincidence!!