What is Screen? From the man page:
"Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)."
How is this handy? I mean, in Linux, you can already use several virtual consoles using alt-function key combinations. Most desktops today utilize a GUI, so you can open multiple terminal windows.
Here's where I ran into a good use case.
The quick explanation is that I had a large file in a remote location to transfer to my local computer. My local computer can see the remote system (with the large file) only if it connects via VPN. The remote system can see my local system through a forwarded SSH connection on my router.
The VPN connection, for reasons I haven't tracked down, acts flaky with my connection. I can connect to the VPN, but if I SSH into a remote system, the session seems to periodically pause or drop, with little reason why. From what I can tell, though, outgoing non-VPN'd sessions seem to work fine.
I need this file, so I connect to the VPN and use rsync over SSH to copy the file. The problem is that the session will hesitate or blink out, just long enough to cause my rsync to disconnect or go wonky.
A little more clever would be to tell the remote computer to copy to my local computer, since that wouldn't go over the VPN. I try that. SSH into the remote system via the VPN, initiate the rsync. But whatever affects my session over the VPN seem to kill my copy job once my terminal is affected.
This is where Screen comes in.
I connect to the VPN. Then I connect to the remote computer via SSH. Then I run Screen with the "screen" command. It looks just like a regular terminal.
The difference is that the shell is running within "screen."
I then start the rsync to my local computer, which is routed not over the VPN but over the public Internet.
I "disconnect" from screen by hitting control-a (control-a is a command that tells screen you want its attention) then "d" for "detach."
I'm dropped back into the base shell. The rsync is still running in the background.
I can exit from my session and drop back to my local computer. Later, I re-connect to the remote computer via SSH and get a list of running Screen sessions by running
screen -ls
...which will give a list of running detached sessions. I reconnect to the session using:
screen -r <session name>
...and I am reconnected, and can see what is currently happening with my running rsync session. If my SSH session is disconnected while in the middle of the screen session I can still reconnect and reattach as described above.
Now when my connection goes wonky, the copy continues uninterrupted! When I'm done, I can use "exit" to close out the shell, which should also close the Screen session if it's the last process in the session. Failing that, control-a k (for kill) should end that Screen window.
Screen can also create other windows using control-a c. A new Screen window is created, and I can start another process there. I can switch among them using control-a n (for next) or control-a p (for previous.)
Alternatively, if you have more than a few windows created, you can switch with control-a <number> to jump to other sessions, numbered 0 through X. You can get a list (and select which to use) by hitting control-a " (yes, the quote sign) and from there use up and down arrows to select the session you want.
To summarize:
screen: starts the program
screen -ls: lists detached, running sessions
screen -r <session>: reattaches to a running session
While in Screen:
control-a d: detaches from a running screen window
control-a k: kill a running screen window
control-a c: create a new window
control-a n: go to the next window
control-a p: go to the previous window
control-a <number>: jump to window <number>
control-a ": list available windows and select from menu which to jump to
control-a ?: display help
control-a A: change the name of the session to something more meaningful for the lists
There are other options available as well, like naming your sessions and displaying a history; these are just a summary of the most useful commands to get started. The man page is your friend!
No comments:
Post a Comment