Prevent pane/window from closing when command completes – tmux

tmux

In my tmux.conf file I have it configured to open windows, name them, setup panes etc.. etc..

However one issue I have is that if one of the panes launches a command, let's say ls, then the pane closes itself after the command completes (instantly). Is there any way to prevent this behavior? Or have it drop to a normal shell after a command completes?

I am assuming that I need to start a shell -> execute command when the pane launches, but I'll be damned if I can figure out how. I have googled a bit for this problem but have come up short.

Best Answer

You have a couple of options.

  1. Instead of running ls in your window, run a shell, then send the shell keystrokes to execute:

    tmux start-server  
    tmux new-session -d -s session  
    tmux new-window -t session:1  
    tmux send-keys -t session:1 ls C-m
    
  2. You can lunch a sequence of commands in such a way as to leave yourself with a bash shell after your other commands have run:

    tmux start-server  
    tmux new-session -d
    tmux new-window 'ls;bash -i'
    
  3. See jasonwryan's answer for details on the remain-on-exit option to keep panes alive after their process has exited so you can review the output.

  4. If the output of some command was worth seeing once, it might be worth refreshing. If you are monitoring the output of something you can watch to periodically get new output. This should play nicely with panes in tmux:

    tmux start-server  
    tmux new-session -d
    tmux new-window 'watch -n 60 ls'
    
Related Question