$ man screen
for more info.
Note: You can do this locally, but more often than not it is used remotely.
Step 1: Install the Software.
$ sudo apt-get install screen
$ sudo yum install screen
Since you are working remotely from the command line using the package manager is usually not an option.
Note: You could install Putty on your MSWindows machines and do the same thing after installing screen on the server. Putty is also available for other platforms.
Step 2: Now Lets Login to a Server.
Unless you are using Putty, go to a terminal and ssh into the server.
$ ssh servername
The you should get something like this if all went well.
if you have not already done so install screen as in step 1.
Step 3: Let's Use Screen.
Here we go:
Typically you’ll execute a command or shell-script as shown below from the command.
$ Linux-command-to-be-executed $ ./Linux-shell-script-to-be-executed
Instead, use the screen command as shown below.
$ screen Linux-command-to-be-executed $ screen ./Linux-shell-script-to-be-executed
Ok, you program is now running. Doing a system update takes a while and is the perfect candidate for screen. such as:
$ sudo apt-get update && sudo apt-get upgrade
Step 4: Detach the Process.
This is where you can let the process go and go to something else or even log out and come back to it.
Once you’ve used the screen command, you can detach it from the terminal using any one of the following method.
Screen Detach Method 1: Detach the screen using CTRL+A d
When the command is executing, press CTRL+A followed by d to detach the screen.
Screen Detach Method 2: Detach the screen using -d option
When the command is running in another terminal, type the command as following.
$ screen -d SCREENIDEither case you will come back to a screen prompt.
Step 5: List Screens in Progress.
On terminal 1 you did the following:
$ screen ./myscript.sh
From terminal 2 you can view the list of all screen processes. You can also detach it from terminal 2 as shown below.
$ screen -ls There is a screen on: 4491.pts-2.FC547 (Attached) 1 Socket in /var/run/screen/S-sathiya. $ screen -d 4491.pts-2.FC547 [4491.pts-2.FC547 detached.]Fail safe:
$ screen -wipe
Step 6: Get Back In.
$ screen -r 4491.pts-2.FC547 What is really neat is that you can log in from any machine, not the original system and restart the screen process after you have re-logged in.Now you are back to work. The screen command has a zillion options. This was just to get your feet wet.
Nice starter reference: http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/.
Step 7: Multiple Windows.
To open a new window, you just use “Ctrl-A” “c”. This will create a new window for you with your default prompt. For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top. (Note: I have truncated some screens to save space.)
Mem: 506028K av, 500596K used, 5432K free, 0K shrd, 11752K buff Swap: 1020116K av, 53320K used, 966796K free 393660K cached
PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME 6538 root 25 0 1892 1892 596 R 49.1 0.3 6614 root 16 0 1544 1544 668 S 28.3 0.3 7198 admin 15 0 1108 1104 828 R 5.6 0.2
Now open a new window with “Ctrl-A” “c”
To get back to top, use “Ctrl-A “n”
Mem: 506028K av, 500588K used, 5440K free, 0K shrd, 11960K buff Swap: 1020116K av, 53320K used, 966796K free 392220K cached
PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME 6538 root 25 0 1892 1892 596 R 48.3 0.3 6614 root 15 0 1544 1544 668 S 30.7 0.3
You can create several windows and toggle through them with “Ctrl-A” “n” for the next window or “Ctrl-A” “p” for the previous window. Each process will keep running while your work elsewhere.
Step 8: A Walkthrough.
Starting Linux Screen
Screen is started from the command line just like any other command: [root@office ~]# screen
Note: you do not have to be root to use it and good security suggests you only use root as a last resort.
You are now inside of a window within screen. This functions just like a normal shell except for a few special characters.
Screen uses the command “Ctrl-a” that’s the control key and a lowercase “a” as a signal to send commands to screen instead of the shell.
For example, “Ctrl-a” then “?”.
Key bindings are the commands the screen accepts after you hit “Ctrl-a”. You can reconfigure these keys to your liking using a .screenrc file, but I just use the defaults.
Command: “Ctrl-a” “c”.
To create a new window, you just use “Ctrl-a” “c”.
This will create a new window for you with your default prompt. Your old window is still active.
For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top.
Now open a new window with: “Ctrl-a” “c”
Your top window is still running you just have to switch back to it.
Switching Between Windows
Command: “Ctrl-a” “n”
Screen allows you to move forward and back. In the example above, you could use “Ctrl-a “n” to get back to top. This command switches you to the next window.
The windows work like a carousel and will loop back around to your first window.
You can create several windows and toggle through them with “Ctrl-a” “n” for the next window or “Ctrl-a” “p” for the previous window.
Each process will keep running until you kill that window.
Detaching From Screen
Command: “Ctrl-a” “d”
Detaching is the most powerful part of screen. Screen allows you to detach from a window and reattach later. If your network connection fails, screen will automatically detach your session! Note this is great when you have run screen on another system and you have to get away or to shut your system down without bothering the system you were attached to.
You can detach from the window using “Ctrl-a” “d”.
This will drop you into your shell.
All screen windows are still there and you can re-attach to them later.
This is great when you are using rsync for server migration.
Reattach to Screen
If your connection drops or you have detached from a screen, you can re-attach by just running:
$ screen -r
This will re-attach to your screen.
However, if you have multiple screens you may get this:
If you get this, just specify the screen you want.
$ screen -r 27153.pts-11.oelt02
Logging Your Screen Output
As a consultant, I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy.
Using “Ctrl-a” “H”, creates a running log of the session.
Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.
Screen can monitor a window for activity or inactivity. This is great if you are downloading large files, compiling, or waiting for output.
If you are waiting for output from a long running program, you can use “Ctrl-a” “M” to look for activity. Screen will then flash an alert at the bottom of the page when output is registered on that screen.
I use this when running a command that takes a long time to return data. I can just fire up the command, switch to another window and not have to keep switching back to check the status.
You can also monitor for inactivity. Why use this?
If you are downloading a large file or compiling a program, you can be notified when there is no more output. This is a great signal to when that job is done. To monitor for silence or no output use “Ctrl-A” “_”.
Locking Your Screen Session
If you need to step away from your computer for a minute, you can lock your screen session using “Ctrl-a” “x”. This will require a password to access the session again.
When you are done with your work, I recommend you stop the session instead of saving it for later. To stop screen you can usually just type exit from your shell. This will close that screen window. You have to close all screen windows to terminate the session.
You should get a message about screen being terminated once you close all windows. [screen is terminating]
Alternatively, you can use “Ctrl-a” “k”. You should get a message if you want to kill the screen.