如何等待一段時間,然後在 bash 腳本中開啟新終端時暫停目前工作

如何等待一段時間,然後在 bash 腳本中開啟新終端時暫停目前工作

我有一個名為我執行的程序mdr-dev,結果打開了連接埠 1022。

打開該連接埠後,我必須讓程式保持運作以保持該連接埠開啟。 問題是它需要一些時間來執行,所以同時我應該等待它打開該連接埠!

一旦連接埠打開,我想打開一個新終端並ssh透過該連接埠連接到遠端設備。

問題是:如何為此編寫 bash 或任何其他腳本?

(我已經嘗試過使用 sleep 命令,但它似乎不起作用。)

這是我到目前為止所嘗試過的:

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

在上面的命令初始化過程之後mdr-dev,我想自動打開一個新終端並運行:

ssh -p 1022 root@localhost

當然,我已經嘗試過:

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

但這似乎不起作用......

我應該如何進行?

答案1

由於我沒有你的mdr-dev程序,所以我很難測試它,所以這裡使用 0.1 版本。請測試並在評論中反饋。

#!/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

相關內容