Tuesday, June 19, 2007

Run program on server after logout(screen)

How to use it (in an easy tutorial fashion)

1. screen %% To open a screen

2. commands %% Run whatever program you wanted to run

3. Ctl+a Ctl+d %% To detach screen

4. screen -x %% To attach screen

5. screen -r pid# %% To attach to the 'pid#' screen if there are more than one screens

========== DONE ==========

GNU screen is a terminal multiplexer, i.e. it lets you run many command-line applications from one console or terminal emulator window.

It is a powerful tool and mainly used in a terminal by a single user who needs to run several programs at once but does not want to log in several times on several terminals - a time consuming business on a dial up connection. Its greatest virtue is that sessions can be detached in one terminal and re-attached in another. This means that you can start a session at the office, go home, log in remotely, and re-attach the session locally. Think of it as the TTY version of VNC. If your dial-up session dies, you can simply reconnect and reattach the screen session - you won't lose any work.

Another underrated benefit is that you can switch between terminals without resorting to the mouse.

It is also very useful as a teaching, demonstration or co-operative working tool. Say you want to share a session with a remote user or show how to use a program. You both have to log into the same computer using the same user name. One user runs the command screen and then the other user runs the command screen -x

If there are multiple screen sessions available, the program will then list the available screen sessions. For example:


   There are screens on:
2463.pts-2.atreus (Attached)
11068.lab (Attached)
2 Sockets in /tmp/screens/S-keithh.

In this case, the user can explicitly specify the desired session using an unambiguous substring of the session name. In the above example, screen -x 11068 and screen -x lab are equivalent and both users will now share the same session. Because each session can contain multiple PTYs, each user can independently switch between the them. They can also share the same PTY in which case they will each see the same display and can each type commands.

Contents

[hide]

[edit] Using Screen

Screen can be controlled while it is running by prefacing commands with the command-key character. By default, this is Control-A. For instance, to view the help screen enter Ctrl-a ? The detach command is Control-a Control-D.

Control-a, Control-c creates a new terminal screen

Control-a, " shows a list of terminal screens

Control-a, n switch to the next screen

[edit] Customizing Screen

The man page for screen details a large number of options but the following examples will illustrate many of the more useful features. By default, screen checks for a ~/.screenrc file and failing that the /etc/screenrc file. Here is my default screenrc:


defflow auto

# Scrollback buffer size in lines
defscrollback 5000

# modify the termcap/terminfo when I'm using XTerm.
terminfo xterm* LP

# Support alternate screens so that, for example, when you
# quit out of vi, the display is redrawn as it was before vi
# redrew the full screen.
altscreen on

# don't display the copyright page
startup_message off

# detach on hangup - if my dial-up session fails, screen will simply
# detach and let me re re-attach later.
autodetach on

msgwait 2 # 1 second messages

# Start a number of initial shells and give them titles which show up in the status-line
screen -t CCase 0 bash
screen -t Sun1 1 bash
screen -t Sun2 2 bash
screen -t CCase 3 bash
screen -t Rich 6 bash
screen -t Pine 7 bash
screen -t Pine 8 bash
screen -t News 9 bash
screen

# remove some stupid / dangerous key bindings
bind k
bind W
bind ^k
bind .
bind ^\
bind \\
bind ^h
bind h
#make them better
bind 'K' kill
#bind 'I' login on
#bind 'O' login off
#bind '}' history
bind 'W' windowlist

I can create an alternate configuration file as follows:


...
sessionname lab # Call this session "lab"

caption always # Leave the status line on permanently
caption string "LAB %t - %w"

msgwait 2 # 1 second messages
screen -t CP 0 bash
screen -t CAC 1 bash
screen -t FTP 2 bash
screen -t xterm 3 bash
screen
...

This can be made easier to use by defining an alias for the command:

  • screen -d -R lab -t LAB -c ~/.screenrc-lab

The -d -R lab option means detach and resume the session named lab and if no such session exists then create it.

The -t LAB option simply puts a title on the status line.

And -c ~/.screenrc-lab specifies the alternate configuration file to use.

No comments: