Search code examples
shellcommandtmuxpane

How to create panes that's won't be destroyed when I quit the command that it's running inside?


I would like to create panes in tmux like so :

$ tmux new-session -d -s mysession "while true; do sleep 1; ls; done" 
$ tmux split-window -h "while true; do sleep 1; ls -l; done"

Running it this way, when I cancel the command that is running in the pane (window) I close it immediately. How to avoid this behaviour ?


Solution

  • Use the remain-on-exit window option to mark the window (and any panes it contains) to remain after the command it runs exits.

    tmux new-session -d -s mysession "while true; do sleep 1; ls; done"
    tmux set-option -t mysession:0 remain-on-exit
    

    When you kill the command, the window will remain, and [the pane] will be labeled "Pane is dead". To restart the same command, issue respawn-window. To start a new command in the window (say, an interactive bash session), issue respawn-window bash.

    respawn-window (and respawn-pane, which I forgot about but can be use to target an individual pane within a window) also take a -k option, which can be used to kill whatever is running in a window and either restart the command or start a new command. You could add something to your .tmux.conf like

    bind-key C-c respawn-pane -k bash
    

    Then, in any active pane, you can type Control-C to kill whatever is running in the pane and replace it with an interactive shell (remain-on-exit would not be necessary in this case, as you are immediately replacing the old command with a new one).