Jumat, 29 Mei 2015

screen: Keep Your Processes Running Despite A Dropped Connection

I guess you all know this: you are connected to your server with SSH and in the middle of compiling some software or doing some other task which takes lots of time, and suddenly your connection drops for some reason, and you lose your work. This can be very annoying, but fortunately there is a small utility called screen which lets you reattach to a previous session so that you can finish your task. This short tutorial shows how to use screen for just this purpose.

Using screen

With screen you can create one or more sessions in your current SSH terminal. Just run screen
Code:
beacon% screen

to start it. This creates a screen session or window (although you don't see it as such) in your current SSH terminal:

Press Space or Return to get to the command prompt:

Looks like your normal SSH terminal, doesn't it?
Now I'm going to describe the most important screen commands that you need to control screen. These commands begin with CTRL a to distinguish them from normal shell commands.
• Ctrl a c - Creates a new screen session so that you can use more than one screen session at once.
• Ctrl a w - See how many screen windows do you have.
• Ctrl a a - Switches to the previous screen window(if you use more than one).
• Ctrl a n - Switches to the window number n+1 (next on the list). Where n is number of the current window. 
• Ctrl a p - Switches to the window number n-1 (previous on the list). Where n is number of the current window.
• Ctrl a d - Detaches a screen session (without killing the processes in it - they continue).
• Ctrl a A - Give your window a unique name.

To close a screen session where all tasks are finished you can type:

Code:
beacon% exit


Now let's play around with it a little bit. In our screen window we run the command

Code:
beacon% top


Now let's create another screen session by typing
Code:
Ctrl a c


A new, blank screen session opens, and there we run ping command for e.g.
Code:
beacon% ping google.com



Now you can browse your two screen sessions by running
Ctrl a n, Ctrl a p , or the simplest way to switch between two recent used screen windows - Ctrl a a.
To see at which window you currently are issue Ctl a w command. The screen will output all the opened shell names in the bottom of the window. The current shell will be marked with asterisk(*).

Going further, let us give names to our windows. Type Ctl a A over the window you would like to rename. The screen will issue prompt "Set window's title to" in the bottom. Set the name you like. For example you can give name "ping" to the appropriate window.

Press Enter. The window is now given a unique name. Type Ctl a w to check that out.

To detach a screen session and return to your normal SSH terminal, type
Code:
Ctrl a d

Back on your normal SSH terminal, you can run
Code:
beacon% screen -ls

to get a list of your current screen sessions:

There is currently 1 screen session:

Code:
23746.pts-40.beacon     (Detached)


In case there is only one screen session you may reattach to it by issuing:

Code:
beacon% screen -r


Suppose, you want to bring up another screen session. All you have to do is to run again "screen" command on the normal ssh prompt.
Code:
beacon% screen

Now, if you detach all screen session and issue "screen -ls" again you will see there are 2 sessions now:
Code:
23746.pts-40.beacon     (Detached)
22140.pts-118.beacon    (Detached)


To reconnect to one of these sessions, run

Code:
beacon% screen -r 22140.pts-118.beacon


where 22140.pts-118.beacon is the name of one of the sessions from the screen -ls output.

My Connection Dropped - What Can I Do?

Now let's assume you edit source code in a screen session, something which normally takes a long time, and suddenly your connection drops. Thanks to screen your work isn't lost. Once your connection is back up, log in to your system with SSH again and run

Code:
beacon% screen -ls

as shown in earlier. From the results pick one session (e.g. 22140.pts-118.beacon) and reattach to it:

Code:
beacon% screen -r 22140.pts-118.beacon

If you picked the right session, you should find your code opened in the editor so that you can continue your work.


https://forums.freebsd.org/threads/screen-keep-your-processes-running-despite-a-dropped-connection.3599/

Tidak ada komentar:

Posting Komentar