23

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
Community
  • 1
  • 1
Swarup Donepudi
  • 984
  • 1
  • 11
  • 23
  • 1
    Can't reproduce. I think the problem is `vagrant up` fails immediately, possibly due to being in the wrong directory. Why don't you try `tmux new-session -d -s rtb123 'vagrant up || echo "vagrant failed" && sleep 60'`. It will sleep long enough on error for you to investigate the situation. – 4ae1e1 Oct 29 '15 at 23:56
  • 1
    You aren't currently attached to session rtb123, so you can't detach from it. Just remove that line. – chepner Oct 30 '15 at 00:55

3 Answers3

27

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.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
mlv
  • 580
  • 4
  • 10
  • 8
    Hi, I'd like to ask, why is the third detach command included? The session is already running detached. Is it necessary? – Karol Pal Aug 06 '19 at 10:15
  • 7
    `tmux send-keys -t rbt123 'vagrant up' C-m`, if there are multiple sessions running. – tejasvi88 Nov 10 '20 at 12:39
13

You are using the -d switch when creating the session. This means that the session will start detached, so you don't need to use the detach command. Besides, if your session is not running when you try to detach, it means that it no longer exists, so your command probably exited.

petersohn
  • 11,292
  • 13
  • 61
  • 98
1

New, detached, named and with command executed in default shell:

tmux new-session -d -s apiserver 'java -cp /root/apiserver.jar com.package.EntryPoint'

To attach use:

tmux attach-session -t apiserver

DEVLAB
  • 11
  • 2