7 ms·
Be Careful Using Tmux and Environment Variables
- dolni 4y agoThis is an interesting bit of behavior but the article did not explain what real-world circumstances make this behavior meaningful.
- cauthon 4y agoIt seems like the behavior would cause problems in the following workflow: - create first session (launch server) - detach - set environmental variables - create second session But I don't understand why you would want to follow this workflow in practice. When using tmux, all my work on that machine is done within tmux sessions, and I expect them to be sandboxed relative to each other. If I want to set an variable that's relevant to a session, I do it from within the session, not outside.
- ajorgensen 4y agoOP here. Apologies for not providing the real world example that triggered the behavior but here's some more background: We have some make tasks that set up a local dev environment and rely on environment variables to control a few things. We ran into an issue where if someone started another tmux session and then ran the make task to start the dev environment the env vars wouldn't be set correctly causing some confusing behavior. Ultimately we decided to use a named tmux socket to ensure the environment variables were picked up correctly when set via the make task but you can also use `set-environment` as well if the ergonomics hit of having to use the named socket each time is too much. It's nice that tmux provides a few work arounds but I thought the original behavior was not intuitive.
- trollied 4y agoSurely you'd run a script as part of your task to make sure it was all set correctly? This just seems like a bad setup.
- bb88 4y agoIf you thought you were reloading the dev environment database, and found out you just reloaded your production database, you would be surprised too. And you would probably wonder why after setting the dev variables, the tmux shell didn't have them. It's one of those rare gotchas that will bite someone in the ass. And it will hurt. And when it does, you'll swear like a mother f'er.
- roughly 4y agoFrom a user perspective, it can look like indeterminate or unexpected behavior - the set of environment variables available within the tmux session depends on the state of the world at the time the tmux server was started, as opposed to when the session was started. The tmux server may start when the session starts, or it may have started much earlier - as a user, unless you're aware of this behavior and paying attention, you won't know. Other posters are right that the "correct" workflow is to set all your environment variables explicitly within the session before launching whatever process you're running, but on an ad-hoc basis, I've often found myself logged into a server and thinking "this may take a long time, I should run this in a tmux session," and this kind of behavior would have (and probably has) bitten me in that case.
- CoolCold 4y agoI've found for myself that it better start tmux/screen session on server up on login and don't spend time thinking on per case basis. So 95% on ssh sessions I use run tmux inside and usually the first command after ssh login is tmux attach. 5% cases are exception when no tmux/screen available or cannot copy my own tmux.conf
- xiwenc 4y agoThe default behavior seems logical to me. At least I think it’s normal for general use cases. I’m curious what was your case that triggered you to dig this up.
- ajorgensen 4y agoOP here. We have some make tasks that set up a local dev environment and rely on environment variables to control a few things. We ran into an issue where if someone started another tmux session and then ran the make task to start the dev environment the env vars wouldn't be set correctly causing some confusing behavior.
- deleted 4y ago[deleted]
- paulryanrogers 4y agoI wonder if it's because of export. If one puts an env var into a command alone then it should be OK. For example, `MY_VAR=1 my-commmand.sh` should leave env vars unchanged for everything else.
- ajorgensen 4y agoYes that should work. In our case we have a few different windows/panes that all want the same environment so it made sense to reach for export as a way to ensure the environment was set but then ran into this quirk of tmux
- ilyt 4y agoI wouldn't rely on env variables. I'd make a script that goes somewhat like script envname command then just loads .env.envname before executing. For k8s work I just put some relevant variables in PS1 so it is always visible "where I am", with prompt looking like this for say "dev" env: [08:15:57] ^ [~] k8s:dev -> ᛯ As for tmux behaviour, it's honestly entirely understandable. tmux have no way of knowing the intent of the user; some people might want "clean" session with defaults of what server is running, other might want some env variables copied. I don't have that problem because I just have session being created in my WM autostart, and I just use that one session 99.99% of the time. And creating new ones usually is from tmux, not from some random shell window elsewhere
- trollied 4y agoI don’t understand. I thought this was obvious? The other way is counter-intuitive.
- ajorgensen 4y agoAt least to me, it seemed most intuitive to not have a global server environment and always copy the environment when starting a new session not just on server start and have the existing behavior when attaching to a session.
- higherhalf 4y ago> tmux show-environment > [..] > -WINDOWIDkj Consequence of using Vim to edit blog posts? It should be, according to author's dotfiles :-P Or maybe using tmux, because I also press kj to determine what pane I'm on sometimes.
- ajorgensen 4y agohahah you caught me!
- anthomtb 4y agoHis solution of using `-L` to create a new named socket for each server does not seem cumbersome to me. I usually run `tmux attach-session` only once per day, so an additional argument would not be a major cognitive load. And I'd imagine most people use shell aliases for that sort of thing anyways (I probably should).
- rudolph9 4y agoThis is actually what I do every time. I thought it was standard practice.
- jhatemyjob 4y agoyea i always do `tmux new -s dev` to start one and `tmux attach -t dev` to attach correct me if im wrong but it seems like i can just do `tmux new -L dev` and `tmux attach -L dev` which is actually better than my old muscle memory. its a win-win for me edit: actually jk it doesnt work. meh who cares i always put my env vars in ~/.profile its weird how OP doesnt mention that at all
- deleted 4y ago[deleted]
- Jtsummers 4y agoThe -L option is for tmux so it needs to go before the command as `tmux -L dev new` and `tmux -L dev attach`.
- jhatemyjob 4y agooh. awesome! thank you kindly edit: damn, now i have to write a custom script for `tmux list-sessions` #!/bin/bash for L in $(ls /private/tmp/tmux-501); do echo $L tmux -L $L list-sessions done honestly at this point it isnt even worth it. i dont wanna have an extra script like this. ill just put my env vars in ~/.profile
- 4y ago
- metafunctor 4y agoThis is something to watch out for especially if you're switching from screen to tmux. When you start a screen session, it will always copy the environment from your shell. When you start a second screen session, it will inherit a possibly different environment. But tmux, on the other hand, spawns a tmux server when you first start a session. It'll copy the environment from your shell. However, from that point onwards, new sessions will use the environment from the server and will not copy the environment from your shell. I like screen's behavior more. A frequent use case is to start a long-running command as a sort of ad-hoc background job. That's really easy with screen (just run screen and run your command, it will always use the parent shell's environment) but relatively easy to screw up with the environment when done with tmux. It kind of works only if you don't have a tmux server already running.
- philsnow 4y agoI have tried on multiple occasions to migrate to tmux (because gestures vaguely "it's the future", screen's codebase is ancient and can still connect directly to a serial port etc), but I just don't like the primitives that tmux gives you ((servers,) sessions, windows, panes). I use a nested setup with screen where each level of nesting has a different escape key, this works really well when I want an inner screen to be on a different machine. screen '-e^Zz' -S meta and then inside that one screen '-e^Oo' -S project1 and for a session on a remote machine mosh some_server -- screen '-e^Oo' -S remote_session_name I haven't figured out a way to get this to work well with tmux, because the local tmux server doesn't know about the remote tmux sessions. Whereas with screen, screen doesn't need to know or care about that. (edit: I use shell aliases, I don't type out the '-e^Oo' etc every time)
- lanstin 4y agoI use backtick, hen two back ticks, then four. Only rarely have to go to eight backticks. :)
- xenophonf 4y ago> screen's codebase is ancient and can still connect directly to a serial port That particular feature is pretty important to me, and it would suck if it got removed. Just because a codebase is ancient doesn't mean it's useless.
- quickthrower2 4y agoThis seems similar to VSCode and it’s shell sessions. You need to restart VSCode to pick up new environment variables rather than just open a new shell. But at least you have a UI in your face to sort of remind you. With tmux server maybe it is less obvious that this will happen.
- kelnos 4y agoI don't think it's like that at all. It's pretty normal and expected that when you change env vars, existing shells won't pick them up. In tmux's case, it starts new shells with "old" env vars, where "old" is "whatever was present when the first tmux session was started".
- quickthrower2 4y agoI mean a new shell within VSCode wont pick up env var changes made after VSCode was launched. So it is roughly analogous: VSCode ~ tmux server VSCode shell ~ tmux pane This is all expected and ergonomic I agree.
- surfsvammel 4y agoTo me, this behaviour makes perfect sense and fits well with my intuition or mental model of how things work. Thanks anyway for a good write up
- Liquid_Fire 4y agoI would have expected that sessions are completely independent from each other, and don't magically inherit some (but not others) environment variables from when the first session was created. (Especially considering the first session might not even exist anymore!) That said, I can understand why it would work this way for simplicity, if all sessions are hosted inside the same process. Luckily for me, I never use more than one session at a time, so this doesn't affect me.
- roomey 4y agoIt helps to understand that tmux is a server, that you connect to with sessions. Your first spawn of tmux starts the tmux server, all your sessions will connect to this server But if it is only one session you are using, w
- douglee650 4y ago`screen` for life, but at this point only for CLI remote sessions. Local terminal guis can p much do everything
- capableweb 4y agoI think the biggest problem I encounter when trying something else than tmux is copy-pasting without having to use the mouse. How does the "local terminal guis" you suggest as alternatives handle that?
- kreetx 4y agoIf the terminal gui is Emacs then it handles copy-paste well ;)
- b3n 4y agoBut at that point you may as well use TRAMP[1] and let Emacs do the multiplexing for you. Yes, (e)shell works over TRAMP too. ;) [1] https://www.gnu.org/software/tramp/ https://www.gnu.org/software/tramp/
- crabbone 4y agoNot quite. I don't think my setup is very special, so, I describe it in more detail: My company provides me with the laptop, VPN connection to the office network, which, in turn, connects to our (tiny) datacenter, where actual work happens (i.e. compilation, CI, testing, all happen there). Sometimes I work from the office, other times from home. The datacenter has several "jump" servers having distinct roles, there is a server that lets you connect to our OpenStack cluster, another has some resources to run a bunch of unrelated VMs in KVM, yet another one is the storage for all kinds of artifacts, s.a. Linux packages, Linux distro images, Docker images for development and testing, and then there's CI cluster. So, my typical setup is like this: I have an ansi-term buffer per jump server. The jump servers are running my tmux session. So, every time I have to move my laptop to a different network (eg. going home from office), I reconnect to the jump servers and to the tmux session they were running so that I can pick up from where I left off before disconnecting from office VPN. If TRAMP could have a persistent session, maybe, I'd not use tmux (I don't like that I have different commands for managing buffer appearance and clipboard). On the other hand, tmux is more universally used (at least in my company), so that sometimes I can simply ask a colleague to connect to my session, if they need to investigate some strange situation (only a handful of people are using Emacs, but almost everyone would be able to use tmux at where I work).
- g0xA52A2A 4y agoIt's rather surprising how nuanced tmux can be considering its OpenBSD roots.
- ajm-codes 4y agoMy domain is ajm.codes and I can't tell you how jealous I am of the two character domain aj.codes Anyways, I'm Not a tmux user but it seems like a nice default behavior to have env. variables persisted
- deleted 4y ago[deleted]
- kagevf 4y agoThis part at the end is something I use a lot to re-attach to a running session: > to re-attach to the second session you’ll need to do tmux -L other attach -t second. I have tmux running on a machine in my closet that I ssh to from a couple of different laptops. I name each session, and then always connect to the same session when I ssh into the closet machine. tmux a -d -t session-name The -d makes the sessions resize to match the current scrren size which is useful for me because I use different screen resolutions on both laptops.
- Jtsummers 4y ago-d causes the other attached clients to become detached from the session. tmux, like screen, limits the size of the rendered window to the smallest presently connected. It happens that it forces a resize to match your current screen size, but that's not its primary purpose.
- kagevf 4y agoAh, OK ... I couldn't get much from the man page, but I recalled that I needed that to get the resize - thank you for the explanation!
- ttyprintk 4y agonew-session -A will automatically create that session upon reboot.
- galleywest200 4y agoWe had this happen at my company. I had set some new memory settings in a JAVA_OPTS environment variable due to heap issues. The DBA kept a tmux server running and would periodically restart our app when they needed to do work on those tables it interacted with during maintenance windows. The heap issues kept occurring even though I triple checked the ENV variables. However, ps -ef showed the old values! Once I figured out he was using tmux the issue was resolved by closing out all sessions and starting new ones.
- devsda 4y agoMay be I misunderstood the article but the tmux behavior I see is slightly different. The env vars from the shell where most recent attach happens are inherited when creating a new pane or window. This is the case even when multiple terminals are attached to the same session. I'm on version 3.3a for reference.
- AnthonyGrayson 4y agoIs this legal information?
- nojs 4y agoIf you want to retain complicated window setups without running multiple sessions concurrently I really like tmuxinator [1]. It lets you declare everything about the session in a config file, and restart the session based only on the file. 1. https://github.com/tmuxinator/tmuxinator https://github.com/tmuxinator/tmuxinator
- linux4dummies 4y ago[dead]
- 752963e64 4y ago[dead]
- mekoka 4y agoI've gotten comfortable with the way tmux works, but the complaints raise a valid point. Its default behavior of prioritizing server environment over session environment is probably the wrong one. Maybe those who don't see this are conflating windows and sessions (I can clearly see this in the comments). Different sessions imply radically different projects, paradigms, contexts. Whereas different windows in a session imply different perspectives, views, within a shared paradigm. Intuitively, it makes sense to be able to start a tmux session after setting up some global (i.e. session wide) environment variables, so that all new windows spawned in that session inherit them. Once you've specified the database or various urls, you don't want to have to source some environment file each time you spawn a window. The session should just already know all this. Inheriting some super global variables (i.e. server wide, machine wide) by default (as opposed to explicitly setting a flag) implies that a shared environment across sessions is more useful than a shared environment across windows within the same session. It's a matter of opinion, taste, or maybe of UX data points, but I suspect that the latter would be favored if there ever was a survey.
- ghshephard 4y agoIn 10+ years, outside of perhaps experimenting with the feature for a few hours to see how it works - I've never created a second session in tmux. I routinely will have 20-25 windows and another 4-6 subpanes inside those windows though. I'm wondering if sessions were more relevant before we all had 30+" monitors on our desk, and panes were less useful. Or perhaps some people have, as you noted, more well defined work/project paradigms that they can associate with a session. With that said - in my day job, we have a really well engineered set of tooling that lets us manage our certificates, ssh keys, and orchestraor/security engine/service discovery tokens - but I'm managing and updating dozens of environments/configs - honestly the environment that I opened even a single pane (inside a window) might shift several times over 5-10 minutes into entirely different clusters - what that pane started with is mostly irrelevant - (and clearly the windows/session and "Tmux Server" environment that I started with is so irrelevant as to be meaningless). About the only global original configurations that I care about and want to carry into my work are bound to my client (/etc/hosts, /etc/resolv.conf, interface, VPN, etc...) The one thing that I've gotten out of this thread is that I probably should be more empathic to other people's workflow - not everyone has the same datacenter/cluster/server environments that I do - and instead of having tooling to manage their environment, they might just rely on what it was when they were launching a "new tmux session at that point in time" (a concept I had never considered until today).
- myself248 4y agoSomewhat related: I had no end of trouble trying to configure my locale on a raspberry pi. Tried everything I could find on the internet, still had inexplicable errors with environment variables not being set or being set differently than I had just set them. It's because my ssh session was bringing variables from my laptop onto the pi: https://stackoverflow.com/questions/2499794/how-to-fix-a-locale-setting-warning-from-perl https://stackoverflow.com/questions/2499794/how-to-fix-a-loc...
- djbusby 4y agoDoes anyone remember Terminator? It was a VTE wrapped in Python IIRC and it allowed very flexible windowing of terminals.
- SeerLite 4y agoI think it's undocumented but you're supposed to be able to have update-environment set to '*' so it always uses the parent shell's environment variables when creating a new session. I have a very quirky setup with tmux and rely on this feature, but it's broken IIRC and add important variables to update-environment manually.
- shaicoleman 4y agoWorks for me, adding the following line to my .tmux.conf makes it inherit environment variables from the parent shell set-option -g update-environment "*"