From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 17210 invoked from network); 2 Nov 2020 08:52:42 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 2 Nov 2020 08:52:42 -0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Message-ID:Date:Content-ID: Content-Type:MIME-Version:Subject:To:References:From:In-reply-to:cc:Reply-To: Content-Transfer-Encoding:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=03gHKCGSrrfRN59MG9KgUu5kJSy42aX4WoJLqAYDgSo=; b=KKBocOqntwBAY9c/iCDwStEdQK J8mbkmV2q0UeeLlnWnI5UkF9pWS6TRrE2KsVM9DPHw8E/J2krMuQjuI/XgzNlvyJh0JyTE5MaeBTA AMcubLyDSoZb+rAm004OEXvsydAjcnf3QxjAq0sDAsV7OKFd6Dg91cJ1imB1CP0+XGg208YULdxGn aDDlxpMpD5E8fXrwHv4G62rQkn1xEtavwYIZ/OwuhvuRXIyhk8zH7UkTr5yPnUx9UVyjt7CrLjT+k kGO9c3Gj/wssXPS6HjjJhBR+kX303NcuRrkspJ0fr7ErFOeoA/RrmTIX5XLpcxIfYUfqGiGTQFVIU FFa5oh6g==; Received: from authenticated user by zero.zsh.org with local id 1kZVZw-0007g8-7V; Mon, 02 Nov 2020 08:52:40 +0000 Received: from authenticated user by zero.zsh.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1kZVZQ-0007JU-40; Mon, 02 Nov 2020 08:52:08 +0000 Received: from [192.168.178.21] (helo=hydra) by mail.kiddle.eu with esmtp(Exim 4.93.0.4) (envelope-from ) id 1kZVZO-0009zj-KZ; Mon, 02 Nov 2020 09:52:06 +0100 cc: zsh-users@zsh.org In-reply-to: From: Oliver Kiddle References: To: Matt Zagrabelny Subject: Re: callback? MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <38421.1604307126.1@hydra> Date: Mon, 02 Nov 2020 09:52:06 +0100 Message-ID: <38422-1604307126.634708@ARf-.jGqp.I3_1> X-Seq: 26142 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: Matt Zagrabelny wrote: > > I am using tmux with zsh. > > I use ssh agent forwarding and I ssh to a remote system that is running tmux. I > then detach from that terminal and my SSH_AUTH_SOCK environment variable > becomes "stale". > > I would like to have zsh execute something to refresh the SSH_AUTH_SOCK > environment variable when I "tmux attach" to the aforementioned detached tmux > session. > > Does anyone know of a way to register a callback or something for zsh so when a > particular command gets run (in my case, tmux attach) my zsh environment > variable can get updated? When "tmux attach" is run, it'll be run by a different instance of zsh to those within the tmux session. It is tmux's child processes that need to receive a callback but I don't think there's any way for tmux to send callbacks. Nor can I think of a particularly sane way that could be implemented in tmux. tmux does provide access to the environment for the current session so you can run something like the following to import a variable into the running zsh: eval $(tmux showenv -s SSH_AUTH_SOCK) You might even set an alias, e.g: alias fixssh='eval $(tmux showenv -s)' In the absence of a direct tmux callback, the easiest may be to detect a stale ssh socket in an existing zsh callback function such as preexec(). There appears to be a TMUX environment variable which can be used to detect a shell that is a a child of a tmux process. So something like the following may work well enough: if [[ $+TMUX ]]; then tmuxrefreshenv() { [[ -r $SSH_AUTH_SOCK ]] || eval $(tmux showenv -s) } add-zsh-hook preexec tmuxrefreshenv fi For something more direct than preexec, you could also try trapping SIGWINCH. tmux will send this to child processes if the pane is resized as a result of tmux attach - which is fairly likely. But it is also sent when the pane is resized for other reasons and may not be sent if you're consistent in your window sizing. Any approach is going to have limitations - it won't pass through subsequent layers of child processes using su, ssh and similar. Another quite different approach would be to use the IdentityAgent option in ~/.ssh/config to ensure a consistent path to the agent socket so that the value remains valid instead of becoming stale. Oliver