要保持SSH会话的活动状态并重新连接,可以使用以下方法:
使用screen
或tmux
工具创建会话:
安装screen
或tmux
:
# 使用Ubuntu或Debian
sudo apt-get install screen
# 使用CentOS或RHEL
sudo yum install screen
# 使用MacOS
brew install screen
# 使用Ubuntu或Debian
sudo apt-get install tmux
# 使用CentOS或RHEL
sudo yum install tmux
# 使用MacOS
brew install tmux
创建一个新的screen
或tmux
会话:
screen
# 或
tmux
在会话中运行你的命令。
按下Ctrl + A
,然后按下D
来分离会话。
当需要重新连接时,运行以下命令:
screen -r
# 或
tmux attach-session
使用nohup
命令在后台运行命令:
nohup your_command &
此命令将命令放在后台运行,并将输出重定向到nohup.out
文件。您可以使用tail -f nohup.out
命令来查看输出。
当需要重新连接时,运行以下命令:
tail -f nohup.out
使用autossh
工具自动重新连接:
安装autossh
:
# 使用Ubuntu或Debian
sudo apt-get install autossh
# 使用CentOS或RHEL
sudo yum install autossh
# 使用MacOS
brew install autossh
运行autossh
命令来保持SSH会话的活动状态:
autossh -M 0 -t your_username@your_server_ip "your_command"
此命令将在远程服务器上运行your_command
,并通过SSH保持活动状态。如果连接中断,autossh
将自动重新连接并恢复会话。
以上是三种常用的方法来保持SSH会话的活动状态和重新连接。根据您的具体需求选择适合的方法。