A more elegant way to train models in the background
Speaking of background training models, the first nohup &
thing that comes to mind must be the nohup & background running program .
But I sometimes encounter bugs.
Here is a more elegant way. tmux
For details, see the Tmux command tutorial! SSH remote training model essential
Install
# Ubuntu 或 Debian
$ sudo apt-get install tmux
# CentOS 或 Fedora
$ sudo yum install tmux
# Mac
$ brew install tmux
New session
# 默认新建一个编号为0的会话
tmux
# 新建一个特定名称的会话
tmux new -s <session name>
detach session
# 注意分离会话并不是退出会话,会话还存在,只不过在后台运行
tmux detach
Terminate session
# 快捷键ctrl+d具有统样的效果,这里就终结会话了,之后无法再次访问
exit
View session
# 分离的会话可通过下面的命令查看
tmux list # or tmux ls
kill session
tmux kill-session -t <name or id>
Reconnect a detached session
tmux attach -t xxx
How to use tmux to train a model
tmux
start a sessionpython main.py &
Run the program in the background during the session. Why should it be run in the background during the session? Because the training model will inevitably output logs, so there is no way to separate the session, so it must be run in the background.tmux detach
detach sessiontmux ls
View sessiontmux attach -t xxx
Connect to the session to view model training results