Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 497
Swarup Donepudi Avatar asked Oct 29 '15 23:10

Swarup Donepudi


People also ask

How do I detach a session in tmux?

Detach From A Session To detach (meaning exit the window to come back to later) from the tmux session, use CTRL + b then d (hold ctrl, press b, let go of both of the keys, and press d). Whatever program(s) you are running in the tmux session will continue going without you.

How do I detach and reattach tmux?

Basic Tmux UsageUse the key sequence Ctrl-b + d to detach from the session. Reattach to the Tmux session by typing tmux attach-session -t my_session .

How do I start a new tmux session?

Using the Prefixes to Control Tmux By default, the prefix is CTRL+B. That is, we have to press the keys CTRL+B and then the command. For example, to create a new session, the command would be C. So, to create a new session we need to press CTRL+B and next C – CTRL+B, C.

How do I open multiple tmux sessions?

You can—and often will—have multiple tmux sessions on a single system, so you want to be able to see what they are. You can also show sessions using the shortcut ctrl–b–s.


Video Answer


1 Answers

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

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

The C-m means hit return.

like image 91
mlv Avatar answered Sep 23 '22 04:09

mlv