7

How I can run a command in Tmux when I'm attaching to a session ?

I want attach and immediately run a command.

I read the docs, but found only send keys, which not suits my needs.

migrated from serverfault.com Aug 15 '13 at 20:23

This question came from our site for system and network administrators.

  • I wonder what you expect to happen if, for instance, the tmux session you attach to happens to be running vi. – Falcon Momot Aug 15 '13 at 8:15
  • It will run only the bash – astropanic Aug 15 '13 at 8:25
9

You can attach to a running tmux session and spawn a new window which runs a particular command:

tmux attach \; new-window vim

Note that this does not spawn vim in the pre-exsiting window - there is no facility for doing that, it doesn't really make sense: as @Falcon Momot points out, an existing window could be running anything, the only way to issue commands is "send keys".

  • 2
    Great answer! This must be obvious to most readers, but it took me a while to realize it, so just in case anyone else has the same question: the \; is an escaped semicolon, used here to separate two tmux commands (without escaping the semicolon new-windows would be treated as a new shell command). – Joe Oct 14 '14 at 10:49
3

I was searching for a solution to this exact problem. It can be done using 'set-buffer' and 'paste-buffer' commands

tmux att -t <session-name> \; set-buffer "<command>^M" \; paste-buffer

Here is a complete example :

# let's start with two sessions running bash
tmux new -s theOtherSession \; detach
tmux new -s astropanic \; rename-window main-window \; detach

# attach to the 'astropanic' session, run a directory listing, output
# current datetime, then detach. Note for carriage return (^M) type ^V^M
tmux att -t astropanic \; find-window main-window \; set-buffer "ls;date^M" \; paste-buffer \; detach

# reconnect to check status
tmux att -t astropanic
  • I did not manage to do carriage return as you said. I had to send the enter key like this: tmux attach -t "$1" \; set-buffer "ls;date" \; paste-buffer \; send-keys C-m. – Alex Palcuie Oct 19 '17 at 22:53
  • 1
    This should be the accepted answer. Basically you should name a window and make the name stick by adding DISABLE_AUTO_TITLE="true" to your shell profile (this is important), then use this window only to run commands upon connecting. I use it to connect to a ssh host and then get my kerberos ticket at the start of the day. ssh hostname -t "tmux a -t 0 \; select-window -t cmd \; set-buffer \"kinit -f -l 1d -r 30d^M\" \; paste-buffer \;". For convenience, give it a nice name and save it as a script in your path. :) Also note the ^M should be inserted by pressing C-v then Enter in your editor. – Gaurav Aug 17 '18 at 17:23
2

It's not clear to me what kind of command you want to run, a tmux command or a shell/OS command. Here's an example of each:

#!/bin/bash

cd

# give the session a name; makes it easier to reuse code lines
_SNAME=Generic

# start a whole new tmux session
tmux new-session -s $_SNAME -d -x 140 -y 35

# can set tmux options
tmux set-option -t $_SNAME default-path /opt/foo/build

# create a new window that's just a shell
tmux new-window -t $_SNAME -n build -d

# create a new window that's running a program
tmux new-window -t $_SNAME -n vim -d vim

This leaves the session unattached. If you want to attach to it, too, add this line at the end of the shell script:

# attach to the new session
tmux attach -t $_SNAME
2

Try out this to run mutt, for example

tmux has-session -t mail
if [ $? != 0 ]
then
tmux new-session -s mail -n mel_GMAIL -d "TERM=xterm-256color ; mutt -F $HOME/.mutt/muttrc_perso"
fi
lxterminal --command="tmux attach -t mail"

It checks the existence of a 'mail' session running, if not, it creates one and starts the application mutt; finally it attaches to it

0

I like to do this:

#!/bin/bash

function tmuxed {
    /usr/local/bin/tmux new-session -d -s bkp 2&>/dev/null
    /usr/local/bin/tmux send-keys "/Users/xxx/bin/thisscript backup" C-m
}

function backup {
    echo 'bla bla doing stuff'
}

$1

And run /Users/xxx/bin/thisscript tmuxed

0

Here is a little script that starts or attach to Tmux and runs a command in it. Once the command has been executed, it'll exit Tmux.

#!/bin/sh -x
SESSION_NAME=foo

tmux has-session -t $SESSION_NAME 2>/dev/null
if [ $? -ne 0 ]
then
  tmux new-session -d -s $SESSION_NAME "$*"
fi
exec tmux attach -t $SESSION_NAME

Usage example:

$ ./script-above 'echo hello world && sleep 10'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.