Install GNU screen (Ubuntu based systems):
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install screen
Create simple session:
coil@coil:~$ screen
this creates new screen session, prompt will look like this:
coil@coil:~$
now we can initiate ssh server to some server to perform some task (in this case continuous running of top command)
coil@coil:~$ ssh coil@lb01
coil@lb01:~$ top
detach from a session without interrupting running process:
Ctrl+a+d
and we get back to
coil@coil:~$ screen
[detached from 2428.pts-7.coil]
List ongoing screen sessions:
coil@coil:~$ screen -ls
There is a screen on:
2428.pts-7.coil (4.11.2019 20:50:18) (Detached)
1 Socket in /run/screen/S-coil.
coil@coil:~$
Reattach (-r flag) the session so we can get back to or running process on remote server (2428 is the session ID from the previous command):
coil@coil:~$ screen -r 2428
We are switched to the ongoing session:
top - 20:16:08 up 1:30, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 99 total, 1 running, 55 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 751172 total, 256648 free, 117828 used, 376696 buff/cache
KiB Swap: 590844 total, 581764 free, 9080 used. 515464 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22260 coil 20 0 44508 4008 3420 R 0.3 0.5 0:02.94 top
1 root 20 0 78036 7304 5264 S 0.0 1.0 0:03.57 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H
Named screen session:
coil@coil:~$ screen -S my_session
coil@coil:~$ screen -ls
There are screens on:
2830.my_session (4.11.2019 21:22:34) (Detached)
2428.pts-7.coil (4.11.2019 20:50:18) (Detached)
2 Sockets in /run/screen/S-coil.
coil@coil:~$
we would reattach to named screen session via its ID or a name:
coil@coil:~$ screen -r my_session
coil@coil:~$ screen -r 2830
Screen is ideal for running critical long running processes on remote servers over ssh. This way process does not exit when our ssh session is interrupted.
Source:
https://www.linode.com/docs/networking/ssh/using-gnu-screen-to-manage-persistent-terminal-sessions/