Using Screen to Manage Multiple Remote and Interrupted SSH Sessions - page 2
Attaching to Remote SSH Sessions
What if I need to start several screen sessions? Let's say I want one to manage a long data load, one to run a build, and one to run a command line bittorrent to download the latest DVD of Suse. If I have run "screen" with no arguments in different terminal windows (or tabs), I will now have three attached screen sessions:
jmjones@dinktrepid:~$ screen -ls
There are screens on:
7957.pts-5.dinktrepid (01/30/2009 07:55:03 AM) (Attached)
7859.pts-3.dinktrepid (01/30/2009 07:54:54 AM) (Attached)
7596.pts-1.dinktrepid (01/30/2009 07:45:37 AM) (Attached)
3 Sockets in /var/run/screen/S-jmjones.
There's a slight problem with this; how can I tell which process ID corresponds to which function? Maybe I can remember which order I started them in and find the right one. But there is an easier way. You can pass screen a certain command line argument that will let you see the text you passed in the "screen -ls" list. Basically, it lets you "tag" a screen session with a piece of text. Most of the time, I use this command to identify my purpose for creating the screen session. Here are three commands to create screen sessions with the same reasons as before, only explicitly declaring the purpose for creating the session.
jmjones@dinktrepid:~$ screen -S data_load jmjones@dinktrepid:~$ screen -S build jmjones@dinktrepid:~$ screen -S torrent
screen uses the text after the "-S" option as the "name" for the session. Now, when I look at a list of screen sessions, I can see the names.
jmjones@dinktrepid:~$ screen -ls
There are screens on:
15966.torrent (01/30/2009 08:06:04 AM) (Attached)
15937.build (01/30/2009 08:05:57 AM) (Attached)
15899.data_load (01/30/2009 08:05:47 AM) (Attached)
3 Sockets in /var/run/screen/S-jmjones.
Detaching from and Attaching to screen Sessions
What do you do when you want to take your laptop home? Normally, closing your Terminal.app or gnome-terminal or konsole results in your processes dying a sad, lonely death. But with screen, you don't have to worry about that. You can detach from the screen session by hitting CONTROL-a followed by CONTROL-d. Most screen commands are accessible by holding down CONTROL-a, letting the keys up, then hitting the next key sequence. In this case, holding down the CONTROL key, then pressing the "a" key and letting it up, then pressing the "d" key (which keeping the CONTROL key depressed) and letting it up will detach the current screen session. After CONTROL-a CONTROL-d, you will drop back into the shell you were in when you started screen.
So, what do you when you get home and want to connect back up to the screen sessions that you detached from at the office? This is where the "screen -ls" command comes in. You have a couple of options to connect back to the "build" session I showed you earlier. You can re-attach by process ID:
screen -r 15937
Or you can re-attach by name:
screen -r build
Creating new windows
When I created individual screen sessions for the build, the data load, and the bittorrent client, it wasn't entirely necessary. All that I really had to do was to create a window (virtual terminal) for each of the applications. From within a running screen session, if you type CONTROL-a CONTROL-c, you will get a new shell in a new virtual terminal (or window) in the screen session. You can create as many virtual terminals as you want. You navigate to the next window by typing CONTROL-a CONTROL-n and you can go to the previous window by typing CONTROL-a CONTROL-p. Or, you can flip back to the last window you were on by typing CONTROL-a CONTROL-a.
Creating named screen sessions definitely has its uses. Being able to identify what you were working on and even collaborating with a co-worker is invaluable. But being able to create multiple "tabs" in a screen session is a huge bonus.
Conclusion
screen will do much more than what I've shown in this article. To get a fuller list of what you can do, try CONTROL-a CONTROL-? from a running screen session or give "man screen" a read. The commands I've shown you are the commands you will probably use most of the time and with them, you will be extremely productive with screen. With these commands, you can create a bundle of shells that you can disconnect from, let them run, then reconnect to when you need to resume your work.
Related Articles
Linux and UNIX How To: Scripting SSH and SFTP
Mastering SSH: Connecting, Executing Remote Commands and Using Authorized Keys
Practical Security: Creating SSH Tunnels
Article courtesy of Enterprise IT Planet
- Skip Ahead
- 1. Attaching to Remote SSH Sessions
- 2. Attaching to Remote SSH Sessions
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.