zsh-users
 help / color / mirror / code / Atom feed
From: Kurtis Rader <krader@skepticism.us>
To: Thorsten Kampe <thorsten@thorstenkampe.de>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Executing command on reattaching to screen session
Date: Thu, 6 Feb 2014 16:37:58 -0800	[thread overview]
Message-ID: <CABx2=D_Q=+cAK8nmkorNjtD9bKrVEqzXyx6eKbnuJ2fZY-n82w@mail.gmail.com> (raw)
In-Reply-To: <CABx2=D9-W87PGOBehUigbBsJ9haX6828TJg4xS3MeKhxQHpR=Q@mail.gmail.com>

[-- 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

  reply	other threads:[~2014-02-07  0:38 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 [this message]
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

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='CABx2=D_Q=+cAK8nmkorNjtD9bKrVEqzXyx6eKbnuJ2fZY-n82w@mail.gmail.com' \
    --to=krader@skepticism.us \
    --cc=thorsten@thorstenkampe.de \
    --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).