Difference between revisions of "Tutorials and How-tos/GNU Screen Quick Tutorial"

From BubbaWiki
Jump to navigation Jump to search
(Page created)
(No difference)

Revision as of 20:34, 7 September 2010

If you manage your server via SSH, the [ttp://en.wikipedia.org/wiki/GNU_Screen GNU Screen] tool can come in rather handy. This nifty utility does two things: it allows you to create multiple remote sessions in one terminal window, and it will keep the sessions running even when you close the SSH connection.

But how can this be useful? When you establish a regular SSH connection from the terminal, the session will remain active only as long as you keep the terminal window open. If you close it, the session terminates along with all the active processes. This means, for example, that if you want to run a backup script on the server, you must keep the terminal window open till the script is done. This is not always practical, especially if the backup operation takes a long time to complete.

This is where the GNU Screen comes into the picture. Using it, you can create a session that will keep running even if you close the terminal window. To install GNU Screen on your server, run the apt-get install screen command. Now connect to the server via SSH, and run the screen command. This creates a new persistent session, and you can use it as a regular terminal window, i.e., issue commands and run scripts. If you want to leave the session without terminating it, use the Ctrl+a d command (press and hold Ctrl, press and hold a, then press d) to detach from the session. To re-connect to the session, use the screen -r command.

Using GNU Screen, you can run multiple persistent sessions identified by their numbers. When you run the screen command, the utility automatically creates a session numbered as 0. To create a new session, use the Ctrl+a c command. You can then switch between sessions using the Ctrl+a n command (to switch to the next session) or Ctr+a 0...9 (to switch to a specific session). If you detached from multiple sessions, and use the screen -r command, it returns a list of running sessions:

 $ screen -r 
There are several suitable screens on:
4870.pts-4.localhost (Detached)
4638.pts-1.localhost (Detached) 

To re-attach to, for example, the first session in the list, use the screen -r command as follows:

 screen -r 4870.pts-4.localhost