From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18340 invoked by alias); 7 Feb 2014 00:38:05 -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: 18409 Received: (qmail 16317 invoked from network); 7 Feb 2014 00:38:01 -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=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=YIb+odgleJfujFdfNVKetnXwXw2Qq4X8hXF9YJjkuPo=; b=XUXeZeETmrhh+CHJK2g6qEUHo2z3ECowEovnjYnPk7JL7AxOSZMLirC8172OHoNoyW 1u6bd6FQV65LaRCEzDRDKfYs1ak6qMw6FkmO8erzpJWe8u6o0rA2tivpiihTCkMQQTI1 rxW8FE+kZCWfKVDn6lyOfTzFumJbUSTkJQCasDVWlaeAeeRgPgK19Awsa0I6JDhUqmqD cpIsG2RsWYNxlEggrCFfyCB+a46vSermktGUdyc8UUQ7pK5DxsAmXt2DPTHnT+CFkg26 771RDIxr90PLBoBCTizFoUpSLh81xVZNwELLahof/gjrfC2sXTjqp2U9Vqkc9JQDQ9NK woaA== X-Gm-Message-State: ALoCoQnYHMP5mVd/2iVF+GQhsT0ylmgN2qXnm43hFnSLN3Zl3rwr4LKXs+qXWXcOSeC0MiO2XIPk MIME-Version: 1.0 X-Received: by 10.66.141.165 with SMTP id rp5mr4064211pab.90.1391733478183; Thu, 06 Feb 2014 16:37:58 -0800 (PST) In-Reply-To: References: Date: Thu, 6 Feb 2014 16:37:58 -0800 Message-ID: Subject: Re: Executing command on reattaching to screen session From: Kurtis Rader To: Thorsten Kampe Cc: Zsh Users Content-Type: multipart/alternative; boundary=001a113312ea96f33204f1c633fe --001a113312ea96f33204f1c633fe Content-Type: text/plain; charset=UTF-8 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 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 > 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 --001a113312ea96f33204f1c633fe--