When you're connected via ssh on a remote server, a sudden disconnection will likely wreck whatever you're working on and make it difficult to find where you left off. This is where screen comes in handy. It's like working in a virtual terminal that will stay open until you exit out of it. If you suddenly lose Internet connectivity at your workstation (power loss at home, office, etc..) you can reconnect to your server, enter your screen session and continue where you left off.
screen isn't the only option, there's other popular ones like tmux and byobo, but being an old schooler, i'm still a fan of screen.
To set it up, ssh into your server and make sure it's installed:
Red Hat based:
Debian based:
Now that it's installed, let's create a simple screen session, disconnect, and reconnect.
Start screen by typing screen. You can add the -S flag to name it which is probably a good habit to get into:
Ok, not very fancy, but you're now within a screen session. Let's start a process that won't stop on its own like:
SO, that should be sitting there pinging a Google ip. Let's pop out of screen for a minute. To leave a screen session on purpose and let it keep doing its thing, you hit the combination of 'ctrl-a d'. That is, hold down the ctrl key while hitting 'a', then let up and hit 'd' (for disconnect).
You should see something like:
Now, you're back in your normal terminal. If not, try the ctrl-a d command again. Then, exit out so you're back on your PC again.
ssh back into your server and list the active screen sessions by typing:
You should see something like:
Since there is only one active, you can re-connect to it by simply typing:
Now, you should see your google ping still going. Hit ctrl-c to end it out.
That screen session would have kept going until you logged back in and stopped it, or if the server went down for some reason.
Now, disconnect from that screen session (ctrl-a d) and start a new one with:
Once in there, disconnect (ctrl-a d) and list out the active sessions:
You should see something like:
Typing 'screen -r' won't work here because it wants you to specify which screen to connect to:
So, we need to do it like this:
Boom, you're back in your original (test1) session!
To kill your sessions, you can reconnect to them, then simply type 'exit' and it will close the session returning you to your normal prompt.
Hope this helps! screen is something I'll use most throughout the day. You can find more information on screen here: https://www.gnu.org/software/screen/
screen isn't the only option, there's other popular ones like tmux and byobo, but being an old schooler, i'm still a fan of screen.
To set it up, ssh into your server and make sure it's installed:
Red Hat based:
Code:
yum install screen
Debian based:
Code:
apt-get install screen
Now that it's installed, let's create a simple screen session, disconnect, and reconnect.
Start screen by typing screen. You can add the -S flag to name it which is probably a good habit to get into:
Code:
screen -S test1
Ok, not very fancy, but you're now within a screen session. Let's start a process that won't stop on its own like:
Code:
ping google.com
SO, that should be sitting there pinging a Google ip. Let's pop out of screen for a minute. To leave a screen session on purpose and let it keep doing its thing, you hit the combination of 'ctrl-a d'. That is, hold down the ctrl key while hitting 'a', then let up and hit 'd' (for disconnect).
Code:
ctrl-a d
Code:
[detached]
Now, you're back in your normal terminal. If not, try the ctrl-a d command again. Then, exit out so you're back on your PC again.
ssh back into your server and list the active screen sessions by typing:
Code:
screen -ls
You should see something like:
Code:
[rob@server ~]# screen -ls
There is a screen on:
24603.test1 (Detached)
1 Socket in /var/run/screen/S-rob.
Since there is only one active, you can re-connect to it by simply typing:
Code:
screen -r
Now, you should see your google ping still going. Hit ctrl-c to end it out.
That screen session would have kept going until you logged back in and stopped it, or if the server went down for some reason.
Now, disconnect from that screen session (ctrl-a d) and start a new one with:
Code:
screen -S test2
Once in there, disconnect (ctrl-a d) and list out the active sessions:
Code:
screen -ls
You should see something like:
Code:
[rob@server ~]# screen -ls
There are screens on:
25439.test2 (Detached)
24603.test1 (Detached)
2 Sockets in /var/run/screen/S-rob.
Typing 'screen -r' won't work here because it wants you to specify which screen to connect to:
Code:
[rob@server ~]# screen -r
There are several suitable screens on:
25439.test2 (Detached)
24603.test1 (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
So, we need to do it like this:
Code:
screen -r test1
Boom, you're back in your original (test1) session!
To kill your sessions, you can reconnect to them, then simply type 'exit' and it will close the session returning you to your normal prompt.
Hope this helps! screen is something I'll use most throughout the day. You can find more information on screen here: https://www.gnu.org/software/screen/