From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12149 invoked by alias); 9 Feb 2014 03:26:21 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18416 Received: (qmail 3527 invoked from network); 9 Feb 2014 03:26:16 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 Date: Sun, 9 Feb 2014 04:20:04 +0100 From: Axel Beckert To: zsh-users@zsh.org Subject: Re: Executing command on reattaching to screen session Message-ID: <20140209032004.GD27889@sym.noone.org> Mail-Followup-To: zsh-users@zsh.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Operating-System: Linux 2.6.32-5-xen-amd64 X-Machine: sym2 x86_64 X-Editor: GNU Emacs 23.2.1 Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAAAAAC3mUtaAAAABGdBTUEAALGPC/xhBQAAADh0RVh0U29mdHdhcmUAWFYgVmVyc2lvbiAzLjEwYSAgUmV2OiAxMi8yOS85NCAoUE5HIHBhdGNoIDEuMindFS5JAAACGElEQVQ4jXXQMU8UYRDG8f8shNjdDH4AbpfGDjAWlKiJiZ0ajL1aGCvsNCbGaCGG1koLaztaTYz6ATy+gOyehYmF3MxVxgg3FnDsHcTpJr/M+8w7Rf6nCsaVTTDqxbg9hoOXmw83H71+Eyfg4E1d7/Z2fG9rGkZbTQiu+K+3U/C+76lmkvAhJuDndnoAiftou4V84okAGclop4U/jYACZDTxrYWP0gkxVfAm/W//GLZpxIzwIN0Hn8dw0B+IWkZmQmRsj2HfhwokEklHfNCCiQCRgAR7YyhQVRVTCKCzP4Y5zBBE0t0zY3Q8oQaBqqAMlVEcgVQd9706zGirAFium8HXumlMIeMwqQCInju+2+uB6MRENupdpMt8pRlHZyuAW0F+Mb6XSIVqtxjD+iVmVqqystLEzFTGT92YqRaXpNT5eTVjeJhbALPnrTxLUZUKZsgxcNm64hAOYisT/xhF+oKTGU5RegtC3Rt6eEDi/QnIevdTx9Md2EMmYBRmCQR1026FCGQQJJExsRUqgkMGaWSbwYLnoO4T6VgpbQbdELPMBAHWWrhYrcxXnYgAsatPWygkFCBD4K62MAsOTqA6szYRPpsu6e6Y8mPiVrBMNuGIMrgwBUu4p2DgG1Ownu6hpuTv7hScefHAzAC/yRRw5U5pALMbJ4AUALvHSZhxgHPXTsHcdWD1GadAHr9avP+c0wCr7263Df8ASLwXWHWs+KIAAAAHdElNRQfYBQEBODPr Organization: DeuxChevaux.org -- The =?iso-8859-1?Q?Citr?= =?iso-8859-1?B?b+tu?= 2CV Database User-Agent: Mutt/1.5.20 (2009-06-14) Hi, On Thu, Feb 06, 2014 at 07:27:43PM +0100, Thorsten Kampe wrote: > tough question: can I automatically execute a command on reattaching > a detached byobu (screen or tmux) session? Does that command needs to be run inside the session itself or does it not need a terminal? I have the later case and simply use a shell script wrapper around screen which does some ssh-agent magic to get the new ssh-agent working with the existing screen session upon reconnect via ssh respectively autossh. Here's the according shell function: asc () { print -Pn "\e]0;%n@%m: autossh -t $* 'screen -RdU'\a" autossh -x -A -t "$@" 'test -x ~/bin/setup-ssh-screen && exec ~/bin/setup-ssh-screen || exec screen -RdU' } And here's setup-ssh-screen: ---snip--- #!/bin/sh HOST=`hostname -s` if [ -n "$SSH_AUTH_SOCK" ]; then # If we ssh in, redirect ~/.ssh/auth_sock to the # (forwarded) ssh agent socket. Keep a different # forwarded socket for each host, since different # vservers do not share the same /tmp (and moving the # socket to my homedir seems to break the socket...) rm -vf ~/.ssh/auth_sock-$HOST echo "Linking SSH auth sock: $SSH_AUTH_SOCK" ln -vs $SSH_AUTH_SOCK ~/.ssh/auth_sock-$HOST export SSH_AUTH_SOCK="$HOME/.ssh/auth_sock-$HOST" fi exec screen -RdU ---snap--- > Every time I login, /etc/motd is displayed. Since I hardly ever log > out and in, I'd like to have the same functionality when reattaching > to my detached session. This could be a simple cat /etc/motd kind of > "autostart". As someone else already proposed, you could use "screen -X". While an "echo" command in a .screenrc is executed also on reattaching to a session, "exec" and "screen" don't seem to be executed a second time, so it seems as if this needs to go into some wrapper script as described above. To not interfere with the input of the running session, I would not use "send-key" or such, but rather use screen's "exec" or "screen" commands. The following two example commands can also be run from inside a screen session to demonstrate their functionality: screen -X exec sh -c 'sleep 2; cat /etc/motd' This may though garble the screen of full-screen text mode applications (like, vim, emacs, aptitude, mc, etc.), but works ok-ish e.g. with just a shell. You usually just need to press enter to get a prompt again. (Just typing the next command also suffices but may feel strange as you don't see a prompt on that line where you're typing.) screen -X screen sh -c 'cat /etc/motd; sleep 2' screen -X screen less /etc/motd This works fine anywhere as it starts a new screen window inside the session which is closed automatically again after 2 seconds or, in the second case, closes when the user presses "q". The "sh -c '…'" is necessary if there are shell meta characters like ";" in the command, because screen passes it's parameters to "execve" or similar and not to "sh -c". HTH Kind regards, Axel -- /~\ Plain Text Ribbon Campaign | Axel Beckert \ / Say No to HTML in E-Mail and News | abe@deuxchevaux.org (Mail) X See http://www.nonhtmlmail.org/campaign.html | abe@noone.org (Mail+Jabber) / \ I love long mails: http://email.is-not-s.ms/ | http://noone.org/abe/ (Web)