Stop worrying about your VPN connection dropping and your remote job to get kill. You can work on a remote server from your laptop or desktop in a safe and controlled way by detaching your screen session and reattaching later.

How to detach a running job from your shell session?

There is a couple of ways to achieve this. The easiest and most common one is probably to just send to background and disown your process. Use Ctrl+Z to suspend a program then bg to run the process in background and disown to detach it from your current terminal session. With the built-in bash job call you can list all the existed backgrounded process, you can use fg to run the process in foreground again as long as it didn’t get detached.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
   bg [jobspec ...]
          Resume each suspended job jobspec in the background, as if it had been started with &.  If jobspec is
          not present, the shell’s notion of the current job is used.  bg jobspec returns 0 unless run when job
          control is disabled or, when run with job control enabled, any specified jobspec was not found or was
          started without job control.

   disown [-ar] [-h] [jobspec ...]
          Without  options,  each jobspec is removed from the table of active jobs.  If the -h option is given,
          each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to  the  job  if
          the  shell receives a SIGHUP.  If no jobspec is present, and neither the -a nor the -r option is sup-
          plied, the current job is used.  If no jobspec is supplied, the -a option means to remove or mark all
          jobs; the -r option without a jobspec argument restricts operation to running jobs.  The return value
          is 0 unless a jobspec does not specify a valid job.

   fg [jobspec]
          Resume  jobspec  in  the  foreground,  and  make  it the current job.  If jobspec is not present, the
          shell’s notion of the current job is used.  The return value is that of the command placed  into  the
          foreground,  or failure if run when job control is disabled or, when run with job control enabled, if
          jobspec does not specify a valid job or jobspec specifies a job that was started without job control.

   jobs [-lnprs] [ jobspec ... ]
   jobs -x command [ args ... ]
          The first form lists the active jobs.  The options have the following meanings:
          -l     List process IDs in addition to the normal information.
          -p     List only the process ID of the job’s process group leader.
          -n     Display information only about jobs that have changed status since the user was last  notified
                 of their status.
          -r     Restrict output to running jobs.
          -s     Restrict output to stopped jobs.

          If  jobspec  is  given,  output  is restricted to information about that job.  The return status is 0
          unless an invalid option is encountered or an invalid jobspec is supplied.

          If the -x option is supplied, jobs replaces any jobspec found in command or args with the correspond-
          ing process group ID, and executes command passing it args, returning its exit status.

How to detach from your active terminal and reattach to a different session?

To detach the job from current terminal and reattach later, you have few options. You can use a screen manager like screen or the terminal multiplexer tmux. Using screen you will simply create a session before running your job using screen -D -R, run your command and leave the screen session Ctrl+A and d.

If you already started a process and for got to run it in a screen or tmux session, then sent it to background and disown it. You can reattach it later from a different tty using retty or reptyr. Note that retty and reptyr are generally not pre-installed on a Linux distribution.

Comments