If you work on external linux machines (production servers, staging servers), then this post is for you.
Suppose there is server, on which an application is running, Now each time you login to the server you need to go to the application path, at the same time you may also want to look at the application logs and access logs.. or may be some other scripts or data directory.
Also if you want to run some script for large data, say 2-3 million entry points, and you don’t want to keep your system on, just because you are connected to the server and if you close the connection from here, the script will stop and the process will be interrupted.
Here is very simple tutorial for you to get started with screens.
Check the version of screen from the terminal.
[root@labalab ~]# screen --version Screen version 4.00.03 (FAU) 15-Dec-08
To start a screen with given name… say webservice, type following command
[root@labalab ~]# screen -S webservice
This will create a screen, the title of the window will show you the screen number, and the path..
[screen 0: bash] root@labalab:~
It showing tilda (~) because we are in user root directory.
Now whatever you do in this screen, will remain on the machine even if you logout, provided the screen should be alive and machine should not be restarted.
Now to detach from screen and come out to the main terminal, just press Ctrl + a + d or type the following command
[root@labalab ~]# screen -d
And it will show you the following message
[remote detached] // OR just [detached]
Now if you want to see the list of all the screens which exist on machine, and which one are attached and which one are detached, just type following command.
[root@labalab ~]# screen -ls // It will show following result There are screens on: 1006.webservice (Detached) 1157.pts-0.labalab (Detached) 3057.user1 (Attached) 3 Sockets in /var/run/screen/S-root.
Now to go back to the screen (reattaching to screen), use
[root@labalab ~]# screen -r webservice
You will be inside screen 0 of ‘webservice’ screen once again.
Now you can create multiple screens within webservice screen,
Follow the commands below:
To create new screen: Ctrl+a+c
To go to next screen: Ctrl+a+n
To go to previous screen: Ctrl+a+p
Next and previous works in cyclic order. If any time you want to exit just one screen, just type exit in the command line.