zsh-users
 help / color / mirror / code / Atom feed
From: Axel Beckert <abe@deuxchevaux.org>
To: zsh-users@zsh.org
Subject: Re: Executing command on reattaching to screen session
Date: Sun, 9 Feb 2014 04:20:04 +0100	[thread overview]
Message-ID: <20140209032004.GD27889@sym.noone.org> (raw)
In-Reply-To: <ld0k6c$a3a$1@ger.gmane.org>

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)


  parent reply	other threads:[~2014-02-09  3:26 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-06 18:27 Thorsten Kampe
2014-02-07  0:31 ` Kurtis Rader
2014-02-07  0:37   ` Kurtis Rader
2014-02-07  0:51 ` Eric De Mund
2014-02-07  4:01 ` Bart Schaefer
2014-02-07 11:54   ` Thorsten Kampe
2014-02-07 16:26     ` Bart Schaefer
2014-02-09  3:20 ` Axel Beckert [this message]
2014-02-13  7:32 ` Sebastian Stark
2014-02-13  8:35   ` Thorsten Kampe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140209032004.GD27889@sym.noone.org \
    --to=abe@deuxchevaux.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).