Stop tmux detached session closing when command finishes - tmux

When I fire up a new, detached tmux session using something like
tmux new-session -d -s "newname" 'python my file.py'
the tmux session seems to disappear from tmux ls once my python program has finished. I'd like to drop back into a shell. How do I do that?

Use send-keys to run your script in the default shell process in the new window.
tmux new-session -d -s "new name"
tmux send-keys -t "new name:0" "python my file.py" Enter

Related

How do i switch the active tmux session inside a shell script

I am trying to create an automated clean shutdown of my opensimulator servers.
On each server, I have several Tmux sessions. Inside each session, there are several windows. In the following example, there are only two sessions to keep things simple.
sara#opensim:~$ tmux ls
Robust: 5 windows (created Tue May 12 22:08:28 2020)
Simulators01: 6 windows (created Tue May 12 23:30:38 2020)
sara#opensim:~$
In the full version, there will be 10+ Simulator sessions.
What I want to do is use a shell script to select a specific session. Every session will have a closedown shell script which looks similar to this:
#!/usr/bin/env bash
SESSION="InstancesTesting"
echo "checking for session - $SESSION"
SESSIONEXISTS=$(tmux list-sessions | grep $SESSION)
if [ "$SESSIONEXISTS" != "" ]
then
echo "session found"
tmux attach-session -d -t $SESSION
tmux select-window -t '0821'
tmux send-keys -t '0821' 'quit' C-m
tmux select-window -t '0900'
tmux send-keys -t '0900' 'quit' C-m
tmux select-window -t '0901'
tmux send-keys -t '0901' 'quit' C-m
tmux select-window -t '0910'
tmux send-keys -t '0910' 'quit' C-m
tmux select-window -t '0911'
tmux send-keys -t '0911' 'quit' C-m
tmux select-window -t '0920'
tmux send-keys -t '0920' 'quit' C-m
echo "finished shut down call for $SESSION"
else
echo "session not found skipping"
fi
The problem line is
tmux attach-session -d -t $SESSION
When it is run from a shell script, everything after that stops until the session is detached. However, without attaching the session only the windows of the last attached session can be accessed.
I can't simply kill the session at the end of the quit commands because the simular running inside each window can take up to 10 mins to shut down. Neither do I want to wait 10 mins between starting each shutdown. I want to set them all going then wait for the processes to close before doing a reboot.
What I need is to either:
1. Attach a session and allow the script to keep running without pressing ctrl+b D to detach.
or
2. Change the session which is being accessed without actually attaching it like the above example.
I have also tried
tmux switch-client SessionName
tmux switch-client -t SessionName
tmux switch-client -n
All of these return the same result
no current client
I have also tried
tmux send-keys -t 'WindowName' 'tmux choose-session' C-m
tmux send-keys -t 'WindowName' '0' C-m
Unfortunately, this option also states there is no client.
I am sure this must be possible, I am going round in circles, please help
There is no concept of a "selected session", clients have an attached session but outside tmux when you don't specify a session the choice of which to use is made separately each time. See here: https://github.com/tmux/tmux/wiki/Advanced-Use#the-default-target
But you shouldn't need it. You are already using -t to specify the window, use it to specify the session as well:
tmux send-keys -t "$SESSION:8021" 'quit' C-m
You don't need select-window either unless you later plan to attach, and then one select-window at the end would do. See https://github.com/tmux/tmux/wiki/Advanced-Use#command-targets for a description of targets.
You may also find the has-session command useful instead of using grep, or the -F flag to list-sessions.

How to automate tmux setup from shell script?

I'd like to setup my tmux session from a shell script.
essentially I want to automate something like:
Create a session, split the window vertically, create a new window, run command 1 in the first vertical split, command 2 in the second, and another command in the second window.
Is that possible? So far I am trying to send keys but I'm not sure if there is a better way.
tmux new-session -s foo -d
tmux new-window -t foo
tmux send -t foo.0 "echo 1" ENTER
tmux send -t foo.1 "echo 2" ENTER
tmux attach -t foo
If you want to be able to later kill the program you are running and be left with a shell, then send-keys is the best bet.
If you don't, you can just start the program directly as part of new-session or new-window, for example:
tmux new-session -sfoo -d -- top
tmux new-window -tfoo -- emacs
tmux attach -tfoo
The remain-on-exit option is also useful for running programs like this.

Tmux loading multiple enviroments from different files

Hello I would like to load multiple environments from different files. For example:
First File builds a session with 1 window and 3 panes.
Second File builds a session with 2 windows first window with 2 panes and second window with one pane.
Like:
tmux -f /path/to/file/basic.conf a
and then after detaching from the first session, i would like to load the same way the other enviroment.
tmux -f /path/to/file/scripting.conf a
but when i fire the second command i will attach to the first session (basic.conf).
But i would expect that when I fire the second script I would attach to the second session.
And tmux ls list only one session.
(The conf files by itself are running with no problem)
How is it possible to have multiple session build trough differnt conf files with tmux, and only with tmux no tmuxinator no tmuxp or anything else?
Or should i have one big conf file which builds everything that now is in muliply conf files?
basic.conf
SESSION_NAME="basic"
FIRST_WINDOW="shells"
SECOND_WINDOW="console"
source ~/.tmux.conf
new-session -s $SESSION_NAME -n $FIRST_WINDOW -d
split-window -h -t $SESSION_NAME
split-window -v -t $SESSION_NAME
new-window -n $SECOND_WINDOW -t $SESSION_NAME
select-window -t $SESSION_NAME:0.0
scripting.conf
SESSION_NAME="script"
FIRST_WINDOW="editor"
SECOND_WINDOW="console"
source ~/.tmux.conf
new-session -s $SESSION_NAME -n $FIRST_WINDOW -d
split-window -v -p 5 -t $SESSION_NAME
send-keys -t $SESSION_NAME:0.0 'cd ~/Code' C-m
send-keys -t $SESSION_NAME:0.0 'vim' C-m
send-keys -t $SESSION_NAME:0.1 'cd ~/Code' C-m
send-keys -t $SESSION_NAME:0.1 C-l C-m
new-window -n $SECOND_WINDOW -t $SESSION_NAME
send-keys -t $SESSION_NAME:1 'cd ~/Code' C-m
select-window -t $SESSION_NAME:0
I think I found a solution by myself.
The command:
tmux -f /path/to/file.conf a
should only be used when you would like to load another tmux config file instead of the default one.
If you like to start multiple sessions , which are preconfigured in files, then you have to do something like this:
tmux source-file -q .dotfiles/tmux/enviroments/basic.conf && tmux attach -t basic
Perhaps there is a better solution, but for now this solves my problem.
P.S. i made a little function to load the files less complicated.
function muxload(){
if [ -f ~/.dotfiles/tmux/enviroments/$1 ]; then
tmux source-file -q ~/.dotfiles/tmux/enviroments/$1 && tmux attach -t $1
fi
if [ -f ~/.dotfiles/tmux/enviroments/$1.conf ]; then
tmux source-file -q ~/.dotfiles/tmux/enviroments/$1.conf && tmux attach -t $1
fi
}
run it like this:
muxload {name_of_conf_file}

Tmux not detaching, sending keys from script

I have a remote server that I can reboot remotely, and I'm trying to make a script that starts my RESTful service. The script is called from .bash_profile. It starts it correctly, but it stays attached to the new tmux session, and hangs until I ssh in and manually detach it. Currently I'm sending C-b d but it doesn't detach.
Here is my small script:
tmux new-session -s cf
tmux send-keys -t cf 'cd ~/server' C-m
tmux send-keys -t cf 'sudo ./gradlew jettyRun' C-m
tmux send-keys -t cf 'C-b d'
How can I achieve this?
you can detach the session when creating it using -d :
tmux new-session -ds cf
and then you can access it anyway

How to send a command to all panes in tmux?

I like to call :clear-history on panes with a huge scrollback. However, I want to script a way to send this command to all the panes in the various windows.
I know how to send a command to all the windows, courtesy of this question, but how do I send a command to all the panes of which window as well?
send-keys and synchronize-panes from the tmux manpage come to mind, but I'm not sure how to marry them together. But maybe there is a simpler way to do this.
Extra Observations:
Thinking about this a little bit, tmux list-panes -a seems to list all the panes in the current session. Pretty useful to start off with. Where do I go from here?
Have you tried following in tmux window with multiple panes
Ctrl-B :
setw synchronize-panes on
clear history
A bit late to the party but I didn't want to set and unset synchronize-panes just to send one command so I created a wrapper function around tmux and added a custom function called send-keys-all-panes.
_tmux_send_keys_all_panes_ () {
for _pane in $(tmux list-panes -F '#P'); do
tmux send-keys -t ${_pane} "$#"
done
}
I also create a wrapper around the tmux command to simplify calling this function (for convenience). The wrapper and the above code are all here.
This allows me to run tmux send-keys-all-panes <command> or tmux skap <command to send <command> to all panes.
Note that tmux is aliased to my wrapper function tmux_pp.
Update June 2019
Quick illustration on how to configure your own binding for synchronize panes.
Added the following into my tmux.conf (the comments certainly apply to my overall configuration):
# synchronize all panes in a window
# don't use control S, too easily confused
# with navigation key sequences in tmux (show sessions)
unbind C-S
bind C-Y set-window-option synchronize-panes
Now, I can toggle the ability to synchronize commands across multiple panes with <C-a><C-y>.
(Yes, I remapped the bind key to Ctrl a).
my tmux version is 1.9a, and this works for me, one key is enough for both on and off
bind-key X set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized,on,off}"
None of the above answers worked for me (tmux v2.3), but this did, from the bash command line:
for _pane in $(tmux list-panes -a -F '#{pane_id}'); do \
tmux clear-history -t ${_pane} ; done
A more generalized script, for tmux commands other than 'clear-history' would just replace that element with a parameter, eg. $1. Do be careful if you intend to write a script to handle a series of tmux commands, as "-t ${_pane}" will need to be applied to each.
Note that the -a parameter to tmux list-panes is required to cover all panes in all windows in all sessions. Without that, only panes in your current tmux window will be affected. If you have more than one tmux session open and only want to apply the command to panes within the current session, replace -a with -s (It's all in the tmux man page).
I haven't the mod points to comment directly on each of the above answers, so here's why they weren't working for me:
The problem that I had with #shailesh-garg 's answer was that the sync affected only commands issued within the panes, not tmux commands issued using Ctrl-B : which are outside the panes.
The three problems that I had with #kshenoy 's answer were that:
it sends keystrokes to within a pane, not to the tmux operation
of that pane, so for instance, if one had a bash shell running in
the pane and one used the script to send "clear-history", those
would be the keystrokes that would appear in the bash command-line.
A work-around would be to send "tmux clear-history" or to pre-pend
"tmux " to "$#", but I haven't edited the answer because of my other
problems with the answer;
I couldn't figure out how to send a
new-line character without literally breaking the line;
Even when I did that, sending "tmux clear-history" had no effect.
If you want to send your command to every pane in every window in every session, add this to your .bashrc:
send_command_to_every_pane() {
for session in `tmux list-sessions -F '#S'`; do
for window in `tmux list-windows -t $session -F '#P' | sort`; do
for pane in `tmux list-panes -t $session:$window -F '#P' | sort`; do
tmux send-keys -t "$session:$window.$pane" "$*" C-m
done
done
done
}
You can then use it like this:
send_command_to_every_pane source ~/.bash_profile
Change "$*" to "$#" if you want that behavior, but in my experience this is what you want.
tmux send-keys -t <session id> <command> C-m
Replace the "session id" and "command" accordingly.
This is my utility function to do it, only executing the command when there there is nothing running in the pane.
#!/bin/bash
_send_bash_command_to_session() {
if [[ $# -eq 0 || "$1" = "--help" ]] ; then
echo 'Usage: _send_bash_command_to_session $session_name what ever command you want: '
return
fi
input_session="$1"
input_command="${#:2}"
for _pane in $(tmux list-panes -s -t ${input_session} -F '#{window_index}.#{pane_index}'); do
# only apply the command in bash or zsh panes.
_current_command=$(tmux display-message -p -t ${input_session}:${_pane} '#{pane_current_command}')
if [ ${_current_command} = zsh ] || [ ${_current_command} = bash ] ; then
tmux send-keys -t ${_pane} "${input_command}" Enter
fi
done
}
tmux_set_venv() {
_current_session=$(tmux display-message -p '#{session_name}')
_send_bash_command_to_session ${_current_session} workon $1
}
Example targeting a session called dev, enabling a python virtualenv in all panes that are in bash or zsh, avoiding executing the command in panes with vim or any other executable:
_send_bash_command_to_session dev workon myvirtualenv
or easier to remember: to do it in the current session:
tmux_set_venv myvirtualenv
Find my configuration file with this function.
You can combine synchronize-panes and send-keys in a single shortcut to send commands to all the panes:
Predefined tmux command clear-history:
bind-key C set-option -w synchronize-panes on\; clear-history \; set-option -w synchronize-panes off
Prompt an arbitrary tmux command:
bind-key p command-prompt -p "Panes command: " "set-option -w synchronize-panes on; %% ; set-option -w -u synchronize-panes"
Prompt an arbitrary shell command:
bind-key p command-prompt -p "Panes command: " "set-option -w synchronize-panes on; send-keys %%\\n ; set-option -w -u synchronize-panes"
By default, byobu uses tmux as backend. It's a wrapper that make things much easier:
Shift+F9:
Ctrl+F9:
Shift+F1
Admittedly only semi-related, I found I could make the status background red when I toggle synchronize-panes so it's obvious when I switch back to a window with an unknown synchronize-panes state:
bind-key C-x setw synchronize-panes on \; set-window-option status-bg red \; display-message "pane sync on"
bind-key M-x setw synchronize-panes off \; set-window-option status-bg default \; display-message "pane sync off"

Resources