brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tmux new -As work attaches to a session or creates it

Tmux
~/tips/tip-shell-tmux-new-as

tmux new -As work

Attaches to the tmux session called work, or creates it when it does not exist.

Every time I log in to a remote machine I want the same tmux session. I do not want to think about whether it already exists.

new-session -A makes new-session behave like attach-session when a session with that name already exists. -s sets the name. Together:

tmux new -As work

First run creates a session called work. Run it again from another terminal, or after an SSH drop, and you are back in the same one. Put it behind an alias, or run it straight over SSH with ssh -t server tmux new -As work. The -t gives tmux a terminal to draw on.

Caveat: attaching does not kick anyone off. If you are already attached from another window, both clients share the session and the size shrinks to the smaller screen. Add -d to detach the other clients first: tmux new -Ads work. And if you do put it in a shell profile that runs at every login, make sure it does not run inside tmux itself, or you end up nesting sessions. Check $TMUX, which is set inside a session.

Further reading: tmux(1) man page, On this site: tmux new -As work attaches to a session or creates it, one command for both, On this site: tmux new -d -s job 'command' runs a command in the background that survives logout, On this site: Guide: tmux for beginners, sessions, windows and panes that survive a dropped connection

More Tmux tips tmux new -d -s report -x 200 -y 50 'top' tmux wait-for build-done set -g allow-passthrough on tmux kill-server Ctrl-b [ All Tmux tips (125) All topics

Link