TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

307 点作者 s0l1dsnak3123超过 2 年前

16 条评论

mindwok超过 2 年前
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 未加载
kennu超过 2 年前
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 未加载
outworlder超过 2 年前
&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 未加载
jaimehrubiks超过 2 年前
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_超过 2 年前
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 未加载
leni536超过 2 年前
&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 未加载
amelius超过 2 年前
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 未加载
1MachineElf超过 2 年前
I feel embarrassed to call myself a tmux fan learning this has been around since at least 2011.
CoolCold超过 2 年前
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 未加载
ivanb超过 2 年前
The ability to attach a detached process to any terminal should be a coreutil.
评论 #33094892 未加载
tpoacher超过 2 年前
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!
johnklos超过 2 年前
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-salt超过 2 年前
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.
meatjuice超过 2 年前
The problem is, however, it works ONLY on Linux.
评论 #33092137 未加载
vasvir超过 2 年前
Very cool. Thanks for posting this. I tried with screen and I can confirm that it also works.
mateusfreira超过 2 年前
Amazing short post ... Yesterday I needed this, what a coincidence!!