News for hackers. Stuff that matters.
Re:How to move a running process into a tmux session (2020) by mindwok on Tuesday October 04, 2022 @09:19PM (#33089803)
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.

Re:How to move a running process into a tmux session (2020) by eftychis on Tuesday October 04, 2022 @09:54PM (#33090081)
This is pretty cool! I had done something similar to connect isolated processes on the fly last year. But never thought to do that with tmux as a target.
Re:How to move a running process into a tmux session (2020) by ynbl_ on Wednesday October 05, 2022 @08:25AM (#33094532)
yes i didnt think theres a way to do this without hacks. unportable and fragile as expected.
Re:How to move a running process into a tmux session (2020) by kennu on Tuesday October 04, 2022 @06:11PM (#33087782)
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.
Re:How to move a running process into a tmux session (2020) by CGamesPlay on Wednesday October 05, 2022 @01:16AM (#33091555)
Seems not super hard to do. A shell function could do the bg, disown, and use the tmux remote API to start a new window and remotely execute reptyr in it.
Re:How to move a running process into a tmux session (2020) by jcynix on Wednesday October 05, 2022 @06:56AM (#33093770)
The streamlined version is what "screen -d …" does, so it's some kind of standard feature, isnt it?. The only "drawback" I see is that you have to plan ahead to use screen.

When I start long running jobs, I try do start them that way, so I can check on them (and their output in a screen logfile) from anywhere:

screen -L -Logfile "batch-xyzzy-221005a" -S "xyzzy1" bash job.sh

Now I can just check the log with grep or less, or reattach to it if some action is needed.

Re:How to move a running process into a tmux session (2020) by kennu on Wednesday October 05, 2022 @07:27AM (#33093979)
I also use tmux beforehand, when I plan ahead. But I think this discussion is about those situations where you didn't plan ahead and still need to somehow keep a process running.
Re:How to move a running process into a tmux session (2020) by kleiba on Wednesday October 05, 2022 @01:52AM (#33091780)
"Do one thing and do it well."
Re:How to move a running process into a tmux session (2020) by mananaysiempre on Wednesday October 05, 2022 @12:34PM (#33097705)
Judging from the amount of infrastructure that’s been built around background text-mode processes, job control does half a thing and sucks at it. It’s still mandatory, so I wouldn’t blame people for wanting to streamline the process of disassociating from that half-baked builtin support and substituting it with a replacement mechanism.
Re:How to move a running process into a tmux session (2020) by outworlder on Tuesday October 04, 2022 @08:05PM (#33089032)
> 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.

Re:How to move a running process into a tmux session (2020) by severino on Wednesday October 05, 2022 @04:58AM (#33092985)
Just for curiosity: what was the purpose of "disown"? If I recall correctly, it was used so the shell you used to launch the process didn't kill it using a "hang" signal or something like that. But I made a quick test and my shell doesn't seem to kill the background process launched from it, at least when I quit using ctrl + d.
Re:How to move a running process into a tmux session (2020) by yrro on Thursday October 06, 2022 @06:08AM (#33106468)
Behaviour depends on your shell. With Bash, for instance:

> The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped. [...] To prevent the shell from sending the SIGHUP signal to a particular job, it should be removed from the jobs table with the disown builtin (see Job Control Builtins) or marked to not receive SIGHUP using disown -h.

> If the huponexit shell option has been set with shopt (see The Shopt Builtin), Bash sends a SIGHUP to all jobs when an interactive login shell exits.

https://www.gnu.org/software/bash/manual/html_node/Signals.h...

Re:How to move a running process into a tmux session (2020) by severino on Tuesday October 11, 2022 @04:29AM (#33160769)
Thanks! I've been doing some tests with the huponexit option but it seems the tricky part was this: when an interactive login shell exits

I think I remember in Linux many years ago (more than 15, maybe) the behavior was present in shells spawned from your DE, but I may be wrong.

Re:How to move a running process into a tmux session (2020) by aumerle on Tuesday October 04, 2022 @11:46PM (#33090943)
Use a terminal like kitty that does not close as long as some launched program is still connected to it. https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.close_on_chi...
Re:How to move a running process into a tmux session (2020) by jagged-chisel on Wednesday October 05, 2022 @06:44AM (#33093679)
How does this compare to using

    nohup <gui-app> &

?
Re:How to move a running process into a tmux session (2020) by jaimehrubiks on Tuesday October 04, 2022 @07:06PM (#33088381)
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.
Re:How to move a running process into a tmux session (2020) by cawwot on Wednesday October 05, 2022 @09:40AM (#33095454)
This is standard practice for systems following STIGs. Bashrc is required to enforce that new sessions always get a tmux and stay in the tmux session.
Re:How to move a running process into a tmux session (2020) by hinkley on Tuesday October 04, 2022 @08:22PM (#33089212)
On a multi user system this seems fraught. That’s probably why it didn’t happen sooner.
Re:How to move a running process into a tmux session (2020) by leni536 on Wednesday October 05, 2022 @02:26AM (#33091979)
> 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...

Re:How to move a running process into a tmux session (2020) by susam on Wednesday October 05, 2022 @03:36AM (#33092436)
Yes! I have a shell function that automatically attaches to an existing tmux session or creates a new tmux session if none exists already, after logging into the remote system via SSH:

  ssht() {
      ssh -t $1 'tmux a || tmux'
  }
Re:How to move a running process into a tmux session (2020) by skhm on Wednesday October 05, 2022 @05:30AM (#33093156)
Don't you find it painful having your tmux session rooted in your homedir (e.g. for all new windows)? I nearly always need to have it anchored some project working directory.
Re:How to move a running process into a tmux session (2020) by mathstuf on Wednesday October 05, 2022 @06:24AM (#33093533)
I have tmux anchored to ~ because I hated new shells inheriting random cwd states. Instead I have a function to cd to "bookmarks" with an easy way to add to it and completion.
Re:How to move a running process into a tmux session (2020) by susam on Wednesday October 05, 2022 @03:23PM (#33100146)
I have the following in my ~/.tmux.conf so that whenever I split pane or create a new tmux window, it sets the current working directory of the new pane or window to the same directory in which I was before:

  # Open new pane or window with the current directory
  bind '"' split-window -c "#{pane_current_path}"
  bind % split-window -h -c "#{pane_current_path}"
  bind c new-window -c "#{pane_current_path}"
So although the tmux session is run in my home directory, I only need to change the current working directory to my project directory once and then when I create new panes and windows, they have the project directory as the current working directory.
Re:How to move a running process into a tmux session (2020) by planede on Wednesday October 05, 2022 @04:17AM (#33092705)
I have an autossh + tmux setup. It's along the lines of

  autossh -M 0 -t remote "tmux new-session -A -D -s $RANDOM$RANDOM$RANDOM$RANDOM"
with appropriate ServerAliveInterval and ServerAliveCountMax settings. It works somewhat nicely with dodgy ssh conncetions too. I'm aware of mosh, but this one even outlives when I put the laptop to sleep. Also mosh doesn't allow X forwarding, and I enable that for sharing the clipboard (yes, I'm aware of the security implications). And if I want to leave something long-running, I can just close the window and reattach later.
Re:How to move a running process into a tmux session (2020) by skhm on Wednesday October 05, 2022 @05:29AM (#33093149)
Mosh should survive a laptop sleep no problem - if it doesn't, something is misconfigured. X forwarding is a dealbreaker though.
Re:How to move a running process into a tmux session (2020) by tpoacher on Wednesday October 05, 2022 @01:48PM (#33098842)
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!

Re:How to move a running process into a tmux session (2020) by _kst_ on Tuesday October 04, 2022 @07:35PM (#33088696)
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.
Re:How to move a running process into a tmux session (2020) by tyingq on Tuesday October 04, 2022 @09:51PM (#33090060)
Either, as root:

# echo 0 > /proc/sys/kernel/yama/ptrace_scope

or permanently by editing the file /etc/sysctl.d/10-ptrace.conf

Re:How to move a running process into a tmux session (2020) by metadat on Tuesday October 04, 2022 @09:56PM (#33090096)
What are the costs and / or downsides to enabling prace_scope?
Re:How to move a running process into a tmux session (2020) by ollien on Tuesday October 04, 2022 @10:45PM (#33090468)
https://askubuntu.com/a/153970

> What are the risks in setting ptrace_scope to 0?

> - This restores the old behavior where one process can "trace" another process, even if there is no parent-child relationship.

> - In theory, a piece of malware can use this to harm you/your computer; e.g. it can attach to Firefox and log all of your URLs/passwords, etc. In practice this is extremely unlikely unless you blindly install binary debs from random sites, etc.

> - As far as debugging goes, the 0 settings is in fact required for gdb, strace, etc. to attach to non-children unless you run them with elevated privileges (sudo).

Re:How to move a running process into a tmux session (2020) by LooseMarmoset on Tuesday October 04, 2022 @09:16PM (#33089762)
were you root?
Re:How to move a running process into a tmux session (2020) by _kst_ on Tuesday October 04, 2022 @11:20PM (#33090744)
No, I wasn't root.
Re:How to move a running process into a tmux session (2020) by CoolCold on Wednesday October 05, 2022 @07:42AM (#33094098)
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.
Re:How to move a running process into a tmux session (2020) by chisquared on Wednesday October 05, 2022 @09:33AM (#33095360)
I think you can just do `tmux new -A`. IIRC this is equivalent to `tmux attach || tmux`.
Re:How to move a running process into a tmux session (2020) by CoolCold on Thursday October 06, 2022 @03:53AM (#33105650)
Could be, will note on that.

In practice I have it via bash alias `tma` , not long command as described. Until I can't bring my .bashrc to particular server of course - there long one, may try your suggestion though

Re:How to move a running process into a tmux session (2020) by ivanb on Wednesday October 05, 2022 @06:56AM (#33093768)
The ability to attach a detached process to any terminal should be a coreutil.
Re:How to move a running process into a tmux session (2020) by IggleSniggle on Wednesday October 05, 2022 @08:54AM (#33094892)
It sort of is? bg / fg / jobs, anyway
Re:How to move a running process into a tmux session (2020) by 1MachineElf on Wednesday October 05, 2022 @12:46AM (#33091370)
I feel embarrassed to call myself a tmux fan learning this has been around since at least 2011.
Re:How to move a running process into a tmux session (2020) by amelius on Tuesday October 04, 2022 @06:59PM (#33088302)
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.)

Re:How to move a running process into a tmux session (2020) by linsomniac on Tuesday October 04, 2022 @07:09PM (#33088403)
Doesn't really feel like the shell would be the right place to do this, as the shell really has no idea, in most cases, of the output history. The whole idea of the scrollback is in the terminal, so it'd probably be, ultimately, the terminal that is the source of this information.

kitty probably has some abilities to do this easily from the shell, as it has all sorts of abilities to interact programmatically with the scrollback history.

Re:How to move a running process into a tmux session (2020) by amelius on Tuesday October 04, 2022 @07:12PM (#33088435)
Good point.

How about starting tmux by default inside a terminal, rather than running the shell directly inside the terminal?

Of course, an ssh session would also need to start tmux.

Re:How to move a running process into a tmux session (2020) by cryptonector on Tuesday October 04, 2022 @11:15PM (#33090701)
No need to `bg` the process. The `reptyr` tool should set it to run once it's gotten ptrace control over it.
Re:How to move a running process into a tmux session (2020) by cbm-vic-20 on Tuesday October 04, 2022 @08:15PM (#33089139)
This sounds like something that mosh was built for.

https://mosh.org/

Re:How to move a running process into a tmux session (2020) by freedomben on Tuesday October 04, 2022 @08:51PM (#33089497)
Mosh helps of course by not killing the child process on timeout, but you'll miss all the console output without something like tmux
Re:How to move a running process into a tmux session (2020) by meatjuice on Wednesday October 05, 2022 @12:57AM (#33091433)
The problem is, however, it works ONLY on Linux.
Re:How to move a running process into a tmux session (2020) by endorphine on Wednesday October 05, 2022 @02:52AM (#33092137)
We'll, not a problem for everyone;)
Re:How to move a running process into a tmux session (2020) by green-salt on Tuesday October 04, 2022 @11:07PM (#33090630)
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.
Re:How to move a running process into a tmux session (2020) by johnklos on Tuesday October 04, 2022 @05:55PM (#33087588)
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.
Re:How to move a running process into a tmux session (2020) by vasvir on Wednesday October 05, 2022 @03:14AM (#33092252)
Very cool. Thanks for posting this. I tried with screen and I can confirm that it also works.
Re:How to move a running process into a tmux session (2020) by mateusfreira on Wednesday October 05, 2022 @06:06AM (#33093412)
Amazing short post ... Yesterday I needed this, what a coincidence!!