Como esperar um pouco e suspender o trabalho atual ao abrir um novo terminal em um script bash

Como esperar um pouco e suspender o trabalho atual ao abrir um novo terminal em um script bash

Tenho um programa chamado mdr-devque executo e que como resultado abre a porta 1022.

Depois de abrir essa porta, tenho que deixar o programa em execução para mantê-la aberta. O problema é que leva algum tempo para ser executado, então, nesse meio tempo, devo esperar que ele abra essa porta!

Assim que a porta estiver aberta, quero abrir um novo terminal e, sshatravés dessa porta, acessar um dispositivo remoto.

A questão é: como escrever um bash ou qualquer outro script para isso?

(Já tentei com o comando sleep, mas parece não funcionar.)

Aqui está o que eu tentei até agora:

#!/bin/bash
echo $MDR_ROOT
mdr-dev --root --mount /opt/tile/home /home --tunnel 1022 22 

Após o mdr-devprocesso de inicialização do comando acima, quero abrir automaticamente um novo terminal e executar:

ssh -p 1022 root@localhost

Claro, eu já tentei:

--tunnel 1022 22 &
sleep 5m 
xterm -hold -e ssh -p 1022 root@localhost

mas isso não parece funcionar...

Como devo proceder?

Responder1

Como não tenho o seu mdr-devprograma, é difícil testar isso, então aqui vai a versão 0.1. Por favor, teste e dê feedback nos comentários.

#!/bin/bash  
#
# This script opens a port to tunnel through, then waits for the tunnel to be opened
# and then connects to the correct host using ssh  
# Answer to: http://askubuntu.com/questions/682316/how-to-wait-for-a-while-and-then-suspend-current-work-on-open-new-terminal-in-a

# Copyright (c) Fabby 2015

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. See the GNU General Public License for more details.
# You DID NOT receive a copy of the GNU General Public License along with this program as the license is bigger then this program.
# Therefore, see http://www.gnu.org/licenses/ for more details.

# version 0.1   dd 2015-10-08   First version.

### Init ###
declare -r bDebug=false
declare szPortOpen=""
if $bDebug ; then
  set -x
  declare iDebugTimeOut=30 #seconds
fi

### Main ###
echo "$MDR_ROOT"
# open tunnel:
mdr-dev --root --mount /opt/tile/home /home --tunnel 1022 22 &

if $bDebug ; then
  read -t $iDebugTimeOut -p "Hit [Enter] to continue..."
fi

#Wait for the tunnel to open
while [ -z "$szPortOpen" ]
do
  sleep 1m
  netstat -atn | grep ":1022" | (read szPortOpen; )
  if $bDebug ; then
    echo $szPortOpen
    read -t $iDebugTimeOut -p "Hit [Enter] to continue..."
  fi
done

# Now ssh to the server 
xterm -hold -e ssh -p 1022 root@localhost

informação relacionada