Keep SSH Sessions running after disconnection

2019-06-14 16:01发布

I am using my laptop via shell terminal to log on school’s server to run a Matlab session. The session will take about 10 hours and I want to close my laptop, go home, have dinner, and re-engage the shell terminal to check the progress of my Matlab session.

From this link I know I should use nohup nohup to keep my terminal alive, but I meet the following problem. Here is a screenshot of my shell after I start running Matlab session:

my matlab session

where a = cv000_29590 is the respond from the Matlab. It should keep running until cv999999 and take about 10 hours.

The problem is, this shell is not interactive anymore. I can’t enter anymore commands, that is, I have no where to enter nohup commend to keep my SSH session alive.

1条回答
戒情不戒烟
2楼-- · 2019-06-14 16:28

It's not really possible after you've already started a session. But for new sessions you can do the following:

  1. Add the following to the top of your .bash_profile:

    if [ -z "${PS1}" ] ; then
        return
    fi
    
    if [ "${TERM}" != "screen" ] ; then
        export HOSTNAME
        exec screen -xRR
    fi
    
    function new {
        u=${1:-$USER}
        test ${u} = ${USER} && screen -t ${u}@${HOSTNAME} || screen -t ${u}@${HOSTNAME} su --login ${u}
    }
    
  2. Put the following content into .screenrc:

    escape ^bb
    shell -$SHELL
    termcapinfo xterm ti@:te@
    hardstatus lastline "%-Lw[%n%f %t]%+Lw%<"
    screen -t ${USER}@${HOSTNAME}
    

    These are mostly my own customizations of screen. The most important of which is that I set the screen escape character to CTRL-b instead of the default CTRL-a so I can still use CTRL-a in bash to go to the beginning of a line.

  3. Use CTRL-b c to create shells in new windows (or just type new at the bash prompt to use the function). And use CTRL-b d to detach your session and leave it running. Next time you login, you'll be reattached to your session and everything will be as it was. Use CTRL-b n to cycle through the windows you've created. If you don't want to multiple windows, you don't have to, just use the ability to leave a session running and reattach later.

查看更多
登录 后发表回答