zsh-users
 help / color / mirror / code / Atom feed
* Executing command on reattaching to screen session
@ 2014-02-06 18:27 Thorsten Kampe
  2014-02-07  0:31 ` Kurtis Rader
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Thorsten Kampe @ 2014-02-06 18:27 UTC (permalink / raw)
  To: zsh-users

Hi,

tough question: can I automatically execute a command on reattaching 
a detached byobu (screen or tmux) session?

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".

Thorsten


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-06 18:27 Executing command on reattaching to screen session Thorsten Kampe
@ 2014-02-07  0:31 ` Kurtis Rader
  2014-02-07  0:37   ` Kurtis Rader
  2014-02-07  0:51 ` Eric De Mund
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Kurtis Rader @ 2014-02-07  0:31 UTC (permalink / raw)
  To: Thorsten Kampe; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1028 bytes --]

It's pretty easy with tmux (via "tmux send-keys") but you need to consider
unwanted side-effects if the session isn't at a shell prompt awaiting
input. What happens if, say, a command like vim is the foreground task when
you try to execute that command? You're just asking for trouble trying to
do it that way. The only time I use that capability is when I know I'm
establishing a new session and want to do some custom initialization (e.g.,
cd'ing to a particular directory and launching my editor).


On Thu, Feb 6, 2014 at 10:27 AM, Thorsten Kampe
<thorsten@thorstenkampe.de>wrote:

> Hi,
>
> tough question: can I automatically execute a command on reattaching
> a detached byobu (screen or tmux) session?
>
> 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".
>
> Thorsten
>
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-07  0:31 ` Kurtis Rader
@ 2014-02-07  0:37   ` Kurtis Rader
  0 siblings, 0 replies; 10+ messages in thread
From: Kurtis Rader @ 2014-02-07  0:37 UTC (permalink / raw)
  To: Thorsten Kampe; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 2526 bytes --]

Below is the function I use as part of a generalized wrapper around ssh and
tmux that does this. The $new_window_command variable is set via an option
to the script (which is named "work") that contains this function. For
example,

work -N "vim ~/docs/snippet" ToDo


Will attach to an existing session named ToDo. If one doesn't exist it will
create a new session and run the command specified by the -N option.

function attach_session {
    local session="$1"
    local rows cols

    # Try to get the size of the tmux virtual terminal so we can resize
    # the local window to match.
    fields='#{session_height} #{session_width}'
    cmd="tmux list-windows -t '$session' -F '$fields'"
    ssh -x -t -p $port $host "$cmd" 2> /dev/null |
        read rows cols
    if [[ $rows == [0-9]## ]] ; then
        rows=$((rows + 1))  # to account for the tmux status line
        size $rows $cols
        cmd="exec tmux attach-session -t \"$session\""
    else
        cmd="exec tmux new-session -s \"$session\""
        if [[ $new_window_cmd != '' ]] ; then
            p1="tmux send-keys -t \"$session\" \"$new_window_cmd\""
            p2="tmux send-keys -t \"$session\" C-m"
            cmd="(sleep 2; $p1; $p2) &; $cmd"
        fi
    fi

    ssh -x -t -p $port $host "exec /bin/zsh -l -c '$cmd'"
}



On Thu, Feb 6, 2014 at 4:31 PM, Kurtis Rader <krader@skepticism.us> wrote:

> It's pretty easy with tmux (via "tmux send-keys") but you need to consider
> unwanted side-effects if the session isn't at a shell prompt awaiting
> input. What happens if, say, a command like vim is the foreground task when
> you try to execute that command? You're just asking for trouble trying to
> do it that way. The only time I use that capability is when I know I'm
> establishing a new session and want to do some custom initialization (e.g.,
> cd'ing to a particular directory and launching my editor).
>
>
> On Thu, Feb 6, 2014 at 10:27 AM, Thorsten Kampe <thorsten@thorstenkampe.de
> > wrote:
>
>> Hi,
>>
>> tough question: can I automatically execute a command on reattaching
>> a detached byobu (screen or tmux) session?
>>
>> 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".
>>
>> Thorsten
>>
>>
>
>
> --
> Kurtis Rader
> Caretaker of the exceptional canines Junior and Hank
>



-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-06 18:27 Executing command on reattaching to screen session Thorsten Kampe
  2014-02-07  0:31 ` Kurtis Rader
@ 2014-02-07  0:51 ` Eric De Mund
  2014-02-07  4:01 ` Bart Schaefer
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Eric De Mund @ 2014-02-07  0:51 UTC (permalink / raw)
  To: zsh-users; +Cc: Thorsten Kampe

Thorsten,

] tough question: can I automatically execute a command on reattaching
] a detached byobu (screen or tmux) session?
]
] 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".

Perhaps screen(1)'s '-X' flag may get you close to what you're looking
for. Note that the word "command" in the description, there, refers to
valid screen(1) commands, not shell commands:

    SCREEN(1)                                                SCREEN(1)
    ...
    COMMAND-LINE OPTIONS
    ...
    -X   Send the  specified command to a running  screen session. You
         can use the  -d or -r option to tell screen  to look only for
         attached or detached screen  sessions. Note that this command
         doesn't work if the session is password protected.

Regards,
Eric
--
Eric De Mund
ead@ixian.com


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-06 18:27 Executing command on reattaching to screen session Thorsten Kampe
  2014-02-07  0:31 ` 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-09  3:20 ` Axel Beckert
  2014-02-13  7:32 ` Sebastian Stark
  4 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2014-02-07  4:01 UTC (permalink / raw)
  To: zsh-users

On Feb 6,  7:27pm, Thorsten Kampe wrote:
}
} 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 

Perhaps a better way to do this is to put something in your precmd
hooks so that the /etc/motd is shown when it has changed?  E.g.,

  latest_motd() {
    if [[ /etc/motd -nt ~/.last_motd ]]
    then
      cp --preserve=timestamps /etc/motd ~/.last_motd
      cat ~/.last_motd
    fi
  }
  add-zsh-hook precmd latest_motd


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-07  4:01 ` Bart Schaefer
@ 2014-02-07 11:54   ` Thorsten Kampe
  2014-02-07 16:26     ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Thorsten Kampe @ 2014-02-07 11:54 UTC (permalink / raw)
  To: zsh-users

* Bart Schaefer (Thu, 06 Feb 2014 20:01:41 -0800)
> On Feb 6,  7:27pm, Thorsten Kampe wrote:
> } 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 
> 
> Perhaps a better way to do this is to put something in your precmd
> hooks so that the /etc/motd is shown when it has changed?  E.g.,
> 
>   latest_motd() {
>     if [[ /etc/motd -nt ~/.last_motd ]]
>     then
>       cp --preserve=timestamps /etc/motd ~/.last_motd
>       cat ~/.last_motd
>     fi
>   }
>   add-zsh-hook precmd latest_motd

Thanks for the suggestion. The information has to be displayed 
regularly even if it hasn't changed.

I was unsure whether to handle the task on the terminal (multiplexer) 
or on the shell level. As far as I can see, the shell doesn't get 
"notified" when I reattach, so it has to be done on the screen/tmux 
level.

The best "approximation" I found so far is to display not on 
reattaching but on idle: I changed the byobu backend to screen, then 
I created a .screenrc in ~/.byobu containing

blankerprg  zsh -c "while true; do cat /etc/motd.d; sleep 30; clear; 
done"
idle        600

That uses screen's screensaver functionality to display text I would 
normally only see on login on a blank screen.

Thorsten


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-07 11:54   ` Thorsten Kampe
@ 2014-02-07 16:26     ` Bart Schaefer
  0 siblings, 0 replies; 10+ messages in thread
From: Bart Schaefer @ 2014-02-07 16:26 UTC (permalink / raw)
  To: zsh-users

On Feb 7, 12:54pm, Thorsten Kampe wrote:
}
} >   latest_motd() {
} >     if [[ /etc/motd -nt ~/.last_motd ]]
} >     then
} >       cp --preserve=timestamps /etc/motd ~/.last_motd
} >       cat ~/.last_motd
} >     fi
} >   }
} >   add-zsh-hook precmd latest_motd
} 
} Thanks for the suggestion. The information has to be displayed 
} regularly even if it hasn't changed.

Well, then ... unless you're using the periodic hook for something else:

    # Show message of the day at least once per hour
    PERIOD=3600
    show_motd() { cat /etc/motd }
    add-zsh-hook periodic show_motd

but I imagine having it as a "screensaver" is still preferable to having
whatever else you were doing scroll away every hour.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-06 18:27 Executing command on reattaching to screen session Thorsten Kampe
                   ` (2 preceding siblings ...)
  2014-02-07  4:01 ` Bart Schaefer
@ 2014-02-09  3:20 ` Axel Beckert
  2014-02-13  7:32 ` Sebastian Stark
  4 siblings, 0 replies; 10+ messages in thread
From: Axel Beckert @ 2014-02-09  3:20 UTC (permalink / raw)
  To: zsh-users

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)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-06 18:27 Executing command on reattaching to screen session Thorsten Kampe
                   ` (3 preceding siblings ...)
  2014-02-09  3:20 ` Axel Beckert
@ 2014-02-13  7:32 ` Sebastian Stark
  2014-02-13  8:35   ` Thorsten Kampe
  4 siblings, 1 reply; 10+ messages in thread
From: Sebastian Stark @ 2014-02-13  7:32 UTC (permalink / raw)
  To: Thorsten Kampe; +Cc: zsh-users

As a side note: if you do this un Ubuntu and expect motd to show the
current system load and such you'll be disappointed. It will update
/etc/motd only on login.

2014-02-06 19:27 GMT+01:00 Thorsten Kampe <thorsten@thorstenkampe.de>:
> Hi,
>
> tough question: can I automatically execute a command on reattaching
> a detached byobu (screen or tmux) session?
>
> 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".
>
> Thorsten
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Executing command on reattaching to screen session
  2014-02-13  7:32 ` Sebastian Stark
@ 2014-02-13  8:35   ` Thorsten Kampe
  0 siblings, 0 replies; 10+ messages in thread
From: Thorsten Kampe @ 2014-02-13  8:35 UTC (permalink / raw)
  Cc: zsh-users

Thursday, February 13, 2014, 8:32:57 AM, you wrote:
> As a side note: if you do this un Ubuntu and expect motd to show the
> current system load and such you'll be disappointed. It will update
> /etc/motd only on login.

That's correct. Ubuntu was actually the targeted distribution for my
question. Fortunately byobu allows to display the system load
dynamically in the status line, so there is no need for a hackish way
to update motd with that stats.

For the motd part itself, I use "run-parts --lsbsysinit
/etc/update-motd.d" instead of a simple "cat /etc/motd".

Thorsten


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-02-13 10:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-06 18:27 Executing command on reattaching to screen session 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
2014-02-13  7:32 ` Sebastian Stark
2014-02-13  8:35   ` Thorsten Kampe

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).