Screen — A must for SSH

If you've ever used SSH to connect to a server, you 'll know its limitations: if you want to open a new window, you'll need to create a second SSH connection to the server. And if the connection breaks during the SSH tunnel, you've lost your progress. This is where Screen comes in.

Screen, which calls itself a “full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” is a usefull tool to use 1 SSH connection, but use multiple screens to work in. So you can have 1 screen to write some scripts in, another to tail a logfile and a third to check your IRC messages ;-)

Screen is installed by default on most installations, you can verify this by running the “which screen” command. If it's not installed, try to apt-get or yum it - it's in most repositories.

You can start screen by typing:

[root@vps ~]# screen

This makes sense, right? :-)
You'll probably notice that not much happens if you type that. At least, it seems like not much as happened. In fact, you've just opened a new “screen” to type your commands. The program “screen” has a few commands of its own, in order to create a new window, and navigate through the open ones.

Once you've opened “screen“, you can see a command list by typing “CTRL + A”, followed by “?” (the question mark). By typing “CTRL + A” you state that the next signal is to be sent to the program “screen“, in stead of to the Shell (like you would in a normal shell). You'll see a list of all bindings known to “screen“.

Start a new window by typing “CTRL + A” + “c”. The C stands for Create - I know, too obvious. A new window will be created. In order to test this, type the command “top”. Then create a new window, by using “CTRL + A” + “c”. You'll see top disappear, and a new window will open. Type in some commands of your choice, and return to the previous window, by doing “CTRL + A” + “n”. The “n” stands for “Next”, and will open the next screen. “CTRL + A” + “p” would've opened the previous screen.

Closing a window, can be done by typing “exit” (like you would in a normal shell). This will cause you to fall back to the previous monitor you opened, or to your main prompt - where you started screen, showing you a message such as “[screen is terminating]” - so you'll know you've hit the main shell.

The biggest advantage in using screen, is that you can “detach” a screen-session. This means you return to the normal shell, but the processes started in “screen” are still active in the background. You can detach yourself by typing “CTRL + A” + “d”. Again, obvious that “D” stands for Detach. This gives you more flexibility for managing your server(s): you can start a number of processes, quietly exit the shell and return a couple of hours later to pick up the session started in screen.

Should you disconnect by accident, during a screen-session, you can always pick up a previous (detached!) screen by relogging to the shell and typing:

[root@vps ~]# screen -ls
There are screens on:
27111.screen_untarring  (Detached)
27097.screen_disking    (Detached)
2 Sockets in /var/run/screen/S-root.

This will show a list of all running screen-sessions at any given time. You can pick up a previous screen-session, by typing

[root@vps ~]# screen -r <name_of_session>

Given the “screen -ls” output from above, you can restore your screen session by typing:

[root@vps~]# screen -r 27111.screen_untarring

It's enough to type in the first unique characters of the screen name, it'll automatically find the session you want. So in essence, “screen -r 271″ would have been enough.

If you see in your list of screens that one session is still attached, you can not resume it before you detach it.

[root@vps~]# screen -ls
There is a screen on:
27097.screen_disking    (Attached)
1 Socket in /var/run/screen/S-root.

[root@vps~]# screen -d 270
[27097.screen_disking detached.]

[root@vps~]# screen -r 270


Kill detached screen jobs:

List screens:

screen -list

Output:

There is a screen on:
23536.pts-0.wdzee       (10/04/2012 08:40:45 AM)        (Detached)
1 Socket in /var/run/screen/S-root.

Kill screen session:

screen -S 23536 -X quit

or come into the screen and then

Ctrl + A, then type ":quit"

More screen tips can be found here: Screen power tips.

                                            

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值