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

From BubbaWiki
Jump to navigation Jump to search
m
Line 1: Line 1:
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.
== What is Screen?  ==


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 nifty utility allows you to access multiple separate terminal sessions inside a single terminal window or remote terminal session.  


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.
<br>


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:
== Why is screen useful?  ==


   $ screen -r  
When you establish a 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.
   There are several suitable screens on:
 
  4870.pts-4.localhost (Detached)
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.
  4638.pts-1.localhost (Detached)  
 
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.
 
<br>
 
== How to install screen  ==
 
1. Access your bubba via SSH (see Howto/Access Server via SSH for information how to accomplish this) Change to the user root: (By default: When asked for password, type excito and press enter)
 
$ '''su'''
 
2. Install the application:
 
$ '''apt-get install screen'''
 
<br>
 
== How to start a new Screen  ==
 
To start an unnamed screen.
 
$ '''screen'''
 
To create a new session with a specific name (eg: ''backup'') use:
 
   $ '''screen -S backup'''
 
These both create a new persistent session, and you can use it as a regular terminal window, i.e., issue commands and run scripts.
 
== Screen commands  ==
 
If you want to leave the session without terminating it, use:
 
  '''Ctrl+a d''' command (press and hold '''Ctrl''', press and hold '''a''', then press '''d''') to detach from the session.
 
<br>
 
== How to List all Screen sessions  ==
 
To re-connect to the session, use ''screen -r''. If multiple sessions are running you get a listing of the screens:
 
   $ '''screen -r'''
There are several suitable screens on:
4870.pts-4.localhost (Detached)
4638.pts-1.localhost (Detached)  
 
Another way to list the screens active is to use the ''screen -list'' command:
 
$ '''screen -list'''
Your inventory:
  4087.radio      (Detached)
  13294.backup    (Detached)
 
<br>
 
== How to attach to a Screen session  ==


To re-attach to, for example, the first session in the list, use the ''screen -r'' command as follows:  
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
   $ '''screen -r 4870.pts-4.localhost'''
 
Or you can to reattach to the 'backup' screen (as long as there is only 1 screen with that name running) you can just use:
 
  $ '''screen -r backup'''
 
<br>
 
== Switching between sessions  ==
 
You can switch between sessions using ''Ctrl+a n'' command (to switch to the next session) or ''Ctr+a 0...9'' (to switch to a specific session).

Revision as of 15:22, 20 October 2010

What is Screen?

This nifty utility allows you to access multiple separate terminal sessions inside a single terminal window or remote terminal session.


Why is screen useful?

When you establish a 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.


How to install screen

1. Access your bubba via SSH (see Howto/Access Server via SSH for information how to accomplish this) Change to the user root: (By default: When asked for password, type excito and press enter)

$ su

2. Install the application:

$ apt-get install screen


How to start a new Screen

To start an unnamed screen.

$ screen

To create a new session with a specific name (eg: backup) use:

 $ screen -S backup

These both create a new persistent session, and you can use it as a regular terminal window, i.e., issue commands and run scripts.

Screen commands

If you want to leave the session without terminating it, use:

 Ctrl+a d command (press and hold Ctrl, press and hold a, then press d) to detach from the session. 


How to List all Screen sessions

To re-connect to the session, use screen -r. If multiple sessions are running you get a listing of the screens:

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

Another way to list the screens active is to use the screen -list command:

$ screen -list
Your inventory:
 4087.radio      (Detached)
 13294.backup    (Detached)


How to attach to a Screen session

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

Or you can to reattach to the 'backup' screen (as long as there is only 1 screen with that name running) you can just use:

 $ screen -r backup


Switching between sessions

You can switch between sessions using Ctrl+a n command (to switch to the next session) or Ctr+a 0...9 (to switch to a specific session).