简体   繁体   中英

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

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 Cm means hit return.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM