32

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.

22

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'
    
  • Thanks man! Awesome answer, number 2 and number 4 are exactly what I was working for, but number 1 seems like a great alternative as well. Thanks for all the help, it's really appreciated (both of you). – Stew Jul 21 '11 at 12:02
  • 1
    The advantage of #1 is that the command remains in the shell history and can be easily re-executed. If the command was big and ugly, then this is very helpful. – goertzenator Sep 17 '13 at 20:38
  • 1
    It is working for me without the start-server command, I think it is started automatically if needed. Is there an advantage of calling it explicitly nonetheless? – luator Nov 13 '17 at 14:30
  • 1
    @luator I think auto-starting a server if none is found might have been added to tmux since I wrote this answer. I don't know of any advantage if your version spawn a server process if necessary. – Caleb Nov 13 '17 at 14:57
13

You could use the remain-on-exit option:

remain-on-exit [on | off]
A window with this flag set is not destroyed when the program running in it exits. The window may be reactivated with the respawn-window command.

To simplify the respawning process, you might want to bind it to a key:

bind-key R respawn-window

This will ensure you aren't left with dead windows when the programs exit.

  • I was getting there! I ran out of time and decided I'd have to edit my answer. Yes, this is the third option, the main disadvantage is that it leaves a read only window that you can't re-use without re-creating. – Caleb Jul 21 '11 at 10:20
  • Good point. I've updated with a (semi-) workaround. – jasonwryan Jul 21 '11 at 10:28
  • Does it work with panes? – azmeuk Jun 19 '17 at 15:25
  • 1
    setting this from the command line can be done with tmux set-option remain-on-exit on – rmanna Jun 14 at 5:42
  • 1
    @azmeuk set-option -g remain-on-exit on bind-key r respawn-pane – HappyFace Aug 13 at 13:31
1

Here is an extended example using the send-keys method recommended by @Caleb

#!/bin/sh
tmux start-server
tmux new-session -d -n 'mywindowname'
tmux send-keys -t mywindowname 'cd notes' Enter 'vim whiteboard/tasks.md' Enter
tmux new-window -d -n 'secondwindowname' # this -d prevents focus from changing to the new window
tmux -2 attach-session -d

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.