Search code examples
bashshellunixvagranttmux

Starting a new tmux session and detaching it, all inside a shell script


I am trying to create a new tmux session and execute the command 'vagrant up'. 'Vagrant up' takes more than 3 hours so I want to detach the session so that I can come back later and check the status of that command by attaching back to the same session.

I followed the answer specified in the StackOverflow post to accomplish the same.

I am getting the error no session found. Here is my code:

    $cat tmux_sh.sh
    #!/bin/bash
    echo "step 1"
    tmux new-session -d -s rtb123 'vagrant up'
    echo "step 2"
    tmux detach -s rtb123

    $./tmux_sh.sh
    step 1
    step 2
    session not found: rtb123

Solution

  • Start a shell, and send vagrant up to it, so you can see the errors.

    tmux new-session -d -s rtb123
    tmux send-keys 'vagrant up' C-m
    tmux detach -s rtb123
    

    The C-m means hit return.