1

I want write a bash script that will:

  • start a new tmux session and run a script
  • attach to it
  • don't 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?

3

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.

| improve this answer | |
  • 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? – MuhsinFatih May 13 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. – Nicholas Marriott May 13 at 19:08

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.