9

I want to write a bash script that will:

  • start a new tmux session and run a script
  • attach to it
  • not exit after the script finishes execution

I tried this:

tmux new -d -s my_session '$SHELL -c " sleep 1; echo please dont exit after echo; sleep 1;"'
tmux attach -t my_session

But this exits after the script finishes execution. How can I keep the session alive so I can attach and execute more scripts?

1 Answer 1

14

You could do one of these:

  • Run a shell after the command: tmux new -dsmy_session 'mycommand; exec $SHELL'

  • Let tmux start a shell and then type the command with send-keys: tmux new -dsmy_session \; send mycommand Enter

You could also set the remain-on-exit option but it is tricky to do this when you create a session with a command because the command might exit before you manage to set it.

2
  • adding exec $SHELL at the end did the trick! Can you explain send-keys though? Is that something I can write inside the shell script? Commented May 13, 2020 at 17:34
  • Yes you can do it like my example, tmux will send the keys as if you had typed them at the shell prompt. Commented May 13, 2020 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .