GNU screen-mitbbs for Linux

Source: Internet
Author: User
Http://hustwindmaple.spaces.live.com/blog/cns! 130e9965613b9994! 8732. entrygnu screen zzgnu screen -- treasure of mitbbs Linux

* What is GNU screen?

We often use our version. Basically, even if we haven't used the screen name, we have heard of it.
What is screen? Why is it the treasure of our town edition?

As mentioned in the screen manual, screen is a terminal multiplexer. But for ordinary people
The definition is similar to that of the non-definition. I think the most common explanation should be that screen is equivalent
A desktop in this interface is like gnome. KDE is used in the X Window System. Secondly, screen also mentions
For functions similar to Remote Desktop (freenx, VNC, or remote desktop in Windows,
You can create a "desktop" in the terminal, and then connect to the "desktop" over the network.
"Windows" are still present. In this regard, screen can replace nohup to help you run remotely for a long time.
Programs, such as simulation that requires overnight computing.

* Basic screen Functions

Before we talk about the basic functions of screen, let's look at a modern desktop, such as gnome,
What does it provide to users.

First, gnome provides a multi-window management mechanism. This was previously provided by metacity, And now many
In this case, it is managed by the compiz fusion 3D Window Manager. Whether it's old metacity or dazzling
Compiz fusion is actually a mechanism for users to manage multiple Concurrently Running images.
Programs. Broadly speaking, the virtual desktop under X is just an extension of this mechanism.

Secondly, gnome provides one (or more) panel, which can be loaded with many useful plug-ins.
. The most useful one is the window list, which shows the names of the applications you are currently opening.
Then, a switchover method is provided.

The second useful panel plug-in is probably system monitor, which can display the current system
Loads and memory. Some people also like to display time, machine name, and other information on the panel.

Next, gnome also provides some other services, such as the copy/paste service between programs,
Set the multi-language encoding mode, keyboard hotkeys, and screen lock when necessary.

We will soon be able to see that all the functions mentioned above have corresponding screen functions. Of course, gnome is
There are countless applications in the blockbuster, namely those that start with G. But these strict
It is not part of a desktop system. Even Nautilus cannot be regarded as a desktop.
An integral part of the header. In fact, many of these applications have their counterparts on the text interface,
It's not the responsibility of screen. I will introduce some text-based applications in this article.
Program.

* Basic screen operations

** Start Screen for the first time

Each screen process itself represents a session. The simplest method is to directly input a session under a terminal.
Screen and press Enter. Then you will see a very simple greeting interface.
The space disappears, and the rest is the normal shell prompt. It seems that nothing has happened. You can
Run some standard shell commands, such as LS, and you will find that this shell has nothing to do with other shells.
.

Run the following command:

Screen-ls

You will find that it tells you that a screen is running:

$ Screen-ls
There is a screen on:
8671. pts-1.inferno (attached)
1 socket in/var/run/screen/S-qiuxing.

Enter Ctrl-A and then enter W in the lower left corner. For example:

0 * $ bash

Press ctrl-A and then press d. Then, the text that appears when you run the command at the beginning is gone. Let's come back now
Run screen-ls once:

$ Screen-ls
There is a screen on:
8671. pts-1.inferno (detached)
1 socket in/var/run/screen/S-qiuxing

Nothing except the status changed from attached to detached.

Now you can turn off the xterm you just used, re-open an xterm, or change to a computer if you have the necessary conditions,
SSH to your first machine, and then enter screen-R. What did you see? Are you the ones before?
Are all texts returned?

In fact, what really happens is this process: screen creates a socket (usually in
/Var/run/screen/), disguise it as a virtual text terminal (PTY device) to act
The standard input/output device then calls a shell program to run it on this text terminal (in fact
Is a socket. When you detach screen from an xterm, the program running under Screen
Will not exit directly, because the standard input device they use is no longer your xterm (
The virtual text terminal, but a socket.

** Manage sessions

In general, we will not create a screen naked. A good practice is a new screen every time
Give it a memorable name:

Screen-s a_name

The next time I use screen-ls, I will find that it has a name (the one behind the random number ).
Points ).

The following commands are related to session management:

-Check the number of screen sessions and their statuses with screen-ls
-Detach a running session with Ctrl-A D
-Use screen-R to reattach the session,
-If you have multiple detached sessions and want to attach them to a specific session, use screen
-R a_name
-Change the name in a running session: press Ctrl-A, and then press Colon: to display a class.
Run the Internal Command sessionname a_name on the input command interface similar to VI.

A common situation: Sometimes we need to force reattach to an attached session, such
I forgot to detach, or the network was disconnected during SSH. The command is screen-Rd.
A_name. In fact, its implementation is to first issue a detach command to the_name session, and then
Generates an attach command.

** Management window

-Generate a new window: Ctrl-a C. This new window runs your shell.
-Generate a new window and run the specified program Ctrl-A:. Then, enter:
Screen-T <Name> <n> <command> where <Name> is the name of the window, <n> is the window number,
<Command> is the specified program. This command is mainly used in the configuration file.
-Close a window: the window is automatically closed when the program exits.
-Force close a window when you cannot exit a program: Ctrl-A K
-Window list: Ctrl-A W
-Window switching:
1. CTRL-a ", you can select a menu
2. CTRL-A <n> and <n> are the window IDs from 0 to 9. This command directly switches to the <n>
3. CTRL-a space, next window
4. CTRL-A backspace, the previous window
5. CTRL-A Ctrl-A: converts the current window to the previous one, which is equivalent to the alt-tab function.
-Window name: Ctrl-

** Split screen

-Up and down are divided into two areas: Ctrl-a s (equivalent to C-x 2 in Emacs
-Switch between the two regions: Ctrl-a tab
-Merge two regions: Ctrl-a q (equivalent to C-x 1 in Emacs) and Ctrl-a x (equivalent
C-x 0 in Emacs)

** Copy/paste

In normal mode, screen transparently passes all commands not starting with Ctrl-A to the window.
The program. In fact, the screen also has a copy/paste mode. After entering this mode, the screen line
To be more like a text editor (VI), you can copy/paste, or move up or down (equivalent
Scroll window ).

-Enter copy/paste mode: Ctrl-a esc or Ctrl-[.
-Move the cursor in C/P mode: Up, down, left, right, and pgup/pgdown. VI hjkl, Be ,/? And so on
And so on.
-Set MARK: space (similar to Ctrl-space in Emacs)
-The content in the two marks automatically enters the C/P buffer of screen and exits the C/P mode.
-This C/P buffer can also be written in a file: Ctrl-A>

In normal mode, Ctrl-A] can be selected by paste.

** Other useful functions/precautions

First, do not press Ctrl-a s. It will lock your shell. But accidentally touch
Don't be afraid of it. Press ctrl-a q and your shell will survive.

The second important thing: I used to jump to the beginning of a sentence using Ctrl-A instead of home in shell/Emacs. Yes
CTRL-A is occupied by screen now. So, how do we enter Ctrl-? Answer: Ctrl-
Add the letter.

Deduction: What if I accidentally attach another screen in one screen? Use CTR-
The screen in a d to detach.

-View system time and other information: Ctrl-A T
-View the screen information: Ctrl-A V
-Lock the screen: Ctrl-A x
-Clear the screen: Ctrl-A L
-Sometimes the screen displays some information. If you want to display the last display prompt:
CTRL-A m
-Video the current window (generate a log to record all information): Ctrl-A H
-Use Visual Bell: Ctrl-A Ctrl-G

* Configuration file

The basic configuration file of screen is ~ /. Screenrc (a hidden file in the user directory ). In fact
The screen parameters controlled here can be entered in the command line mode in a running screen.
(CTRL-A :), and enter the relevant command to implement it. However, writing configuration files can save a lot of repetitive work.

When screen is started, a "-c" parameter is used to use any specified file as the configuration file. For example:

Screen-C. screenrc2

There is a screenrc file example in the appendix, which details what each line is.

* Advanced usage

** Follow a window

Sometimes we may open a window to run a program that will take a long time, or the window that runs is
Pine/mutt, but we work in another window. In most cases, the first window does not have any
Information, but if there is a change, for example, the program is running out or a new email is received, we also hope
Enough to get a prompt in the current window.

There is another opposite situation, such as compiling a large program and constantly generating various information. However, for example
If it fails, it means that the compilation is complete (or wrong). At this time, we also hope to be notified. This
Functions are the most useful for Gentoo users.

You can enable or disable monitoring.

-Ctrl-a m. If the current window changes, the system will notify you through message.
-Ctrl-A _. It is the opposite of the above. If the current window does not work for 15 seconds, screen will send
A msg.

** Caption/hardstatus line

Previously I mentioned that screen can implement functions similar to GNOME panel. This function is available in screen.
It is called caption line or hardstatus line. Its configuration seems complicated, but once it is clarified
You can use it to do many cool tasks. An analogy is to configure the bash prompt and also belongs
Is not well configured but very powerful.

Let me give you a simple example:

Caption always "% {+ B YK} % W"
Hardstatus alwayslastline "Host: % H load: % L"

The effect is that two Panels appear. The first one is caption line, and the content in it is colored.
Window information. The second is hardstatus line, which contains the host and system load information.

In "% {+ B YK} % W", % indicates escape sequence, + B Indicates bold, and YK indicates bright yellow (y,
With a black (k) background, % W is the window number/name. In the hardstatus bar, % H is hostname and % L is
System load (the same as shown in top), others are general characters.

The following is a complicated hardstatus line configuration, with a better visual effect than % W:

Hardstatus alwayslastline '% {YK} %-w % {. yk} % N % T % {-} % + w % = % {. w }'

For more color information, see the chapter about string escapes in screen manual.

** Panel plugin

Although screen provides many string escapes, they still cannot meet all requirements of everyone.
Yes. For example, I want to know how much memory the system uses, and some may want to check whether there is any new
Emails, etc. In GNOME panel, these are implemented through the panel plug-in. In Screen
The caption/hardstatus line also has a similar mechanism, allowing you to put any specified program
The output is placed on the panel.

A simple example:

Backtick 1 5 5 uptime
Hardstatus alwayslastline "% 1 '"

Explanation: backtick 1 5 5 uptime defines the standard output of the external command uptime
A string escape with a ID of 1. It is displayed for 5 seconds at a time, and then run again after 5 seconds.

A complicated example (this is also the screenrc I am using ):

Backtick 1 5 5/home/qiuxing/bin/my_sysload1
Backtick 2 5 5/home/qiuxing/bin/my_free

Hardstatus string '% {YK} %-w % {. yk} % N % T % {-} % + w % = % {. W }% {. yr} % 1' % {. yb} % 2'm %
{Ky} % C'

The contents of my_sysload1 and my_free are as follows:

My_sysload1:

#! /Bin/bash
CAT/proc/loadavg | cut-d ""-F1

My_free:
Free-M | tail-N 2 | head-N 1 | cut-c27-29

** Color 256

Compared with the new screen, the system supports 256 colors. If you compile it yourself, remember to enable this option during compilation.
This compilation option is not added to feisty and the screen in Ubuntu, but in gutsy, this option has
It is successful. I know programs supporting 256 colors include VI/Emacs/elinks. If you know other Text Models
The program supports 256 colors. Please let me know.

If you know that your screen supports 256 colors, add the following statements in. screenrc:

Term xterm-256color
Attrcolor B ". I"
Termcapinfo xterm-256color 'co #256: AB =/E [48; 5; % DM: AF =/E [38; 5; % dm'
Defbce "on"

The first sentence made the screen disguised as a xterm-256color, and the second sentence made the screen use the bold font,
The third sentence tells screen how to define the foreground background and other information. The last sentence is to enable screen to use the background color.
Erase text.

Also, check that your system does have information about the term xterm-256color. In ubuntu,
This library is called ncurses-term.

** X/mouse support

Some text programs also support the mouse (Emacs, elinks, w3m), and some even support the inline
Picture Display (w3m ). Screen supports these features well. Just remember the time of remote connection
When X11 forwarding is enabled (ssh-x), check whether the shell variable display is not
Is set correctly (local display should be: 0.0, remote usually: 10.0 and so on ).

** Encoding conversion

Screen also has a little-known function: Implements encoding conversion. For example, mrxvt can only support GBK,
Unicode is not supported. However, my Chinese files are all unicode encoded. In this case, you can use
Screen to achieve encoding conversion:

Encoding GBK UTF-8

You only need to generate a screen session, detach, and
Reattach the session in the mrxvt encoded as zh_cn.gbk.
Zh_cn.utf8-Encoded chinese text is automatically converted to GBK format.

** Multi-user support

Screen supports multiple users. That is to say, a user generates a session and
To participate. The specific method is to first turn screen into a setuid root program, and then
The screenrc allows another user to connect.

If you have used this function, you will find that it is cool. But because of security concern,
I will not write too much detail. For more information, see Gentoo HOWTO:

Http://gentoo-wiki.com/HOWTO_Snoop_terminal_session#Screen

* Appendix

** Example of a screenrc File

# Color-related configuration
Term xterm-256color
Attrcolor B ". I"
Termcapinfo xterm-256color 'co #256: AB =/E [48; 5; % DM: AF =/E [38; 5; % dm'
Defbce "on"

# Encoding and conversion
Encoding GBK UTF-8

# Use Visual bell
Vbell on

# Automatic detach During disconnection
Autodetach on

# Don't come out with that annoying greeting
Startup_message off

# Make the shell in every window a login shell
# Indicates that the task will be executed ~ /. Bash_profile
Shell-$ Shell

# In copy/paste mode, you can trace back to the history of rows 5000.
Defscrollback 5000

# Remove some stupid/dangerous key bindings
Bind K
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

# Default Windows

Screen-t work 0
Screen-T Shell 1
Screen-T su 3 sudo Su-
Screen-T others 4
Chdir/home/qiuxing/documents/writing/Linux
Screen-T notes 5
Chdir
Screen-T mutt 6 mutt
Select 0

# Hardstatus line hack
Backtick 1 5 5/home/xqiu/bin/my_sysload1
Backtick 2 5 5/home/xqiu/bin/my_free

Hardstatus alwayslastline '% {YK} %-w % {. yk} % N % T % {-} % + w % = % {. W }% {. yr} % 1' % {.
Yb} % 2'm % {Ky} % C'

** Common text mode programs

-File Manager:
1. Bash
2. MC (midnight Commander)
-System resource management
1. Top
2. htop
3. Ps
-Text editing: emacs-NW, Vi, Nano ,...
-Web Browser: w3m (excellent Chinese support), elinks
-Chat:
1. Finch. This is the text mode of pidgin/Gaim. the only text mode program that supports QQ
2. centericq was used a long time ago and has powerful functions.
3. irssi best irc chat room Client
4. irssi + bitlbee if you use this combination, you are the real chat geek!
-Email/newsgroup
1. Mutt
2. Pine
3. gnus
-File Transfer
1. lftp (supports many protocols including SFTP and WWW)
2. wget
3. rsync
4. unison
-Scientific operations: R, Ave ave, maxima, Python (scipy ),...
-Network debugging
1. tcpdump
2. NMAP
3. Netcat
-The music mpg321 and mplayer are also complicated, such as cplay and MOC.
-BitTorrent: rtorrent

** More information

The simplest entry: http://blogamundo.net/code/screen/
A more complex: http://gentoo-wiki.com/TIP_Using_screen
Of course, the most comprehensive information is screen User's Manual:
Http://www.delorie.com/gnu/docs/screen/screen_toc.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.