Sometimes it is necessary to keep the command-line interface open for an extended period. In this time-frame, you need to make sure that the system does not time you out, or even if you have an internet issue, your session remains uninterrupted. The "screen" command is perfect for the occasion as it opens another shell, within the current session, allowing you to run applications or programs in the background. It is perfect if you want to start a download process that you estimate is going to take at least 2 hours. It is fantastic when you are executing a script that needs to run for an extended time. For example, you want to generate a new RSS feed for your website, or you are running an online store, and you need to update the prices of all your 10000 products. These are usually tasks that take much time and making sure they run until the very end could be essential to your business. Using the "screen" command is very easy. All you need to do is log into your hosting account via SSH and then type:
Upon doing so, your current SSH session is going to appear as if it just reloaded. When this happens, you may start your long downloads or execute your long-running scripts. After doing so, to return to your first shell, you can press Ctrl + a + d sequence in quick succession. When you press this combination, you are going to receive the following output from the command-line:
[detached from 8924.pts-0.ger1]
The string following the "detached from" is randomly generated, so do not pay much attention to it. What's important is the fact that you have now detached from the screen. All processes you started within the screen are running in the background. To return to your screen and check if the tasks you started have been completed, please type:
This command is going to take you back to the "screen" immediately. To terminate it, please type "exit" while inside the "screen" itself. Doing this is going to generate the following output:
[screen is terminating]
You will NO LONGER be able to return to this screen, so please, be mindful when exiting.
Sometimes, you need to start more screens at the same time. The "screen" command-line tool allows you to create as many shell sessions as you need to, and it gives you the option of naming them with specific names relevant to the tasks they are going to run. To do so, use the "-S" flag:
screen -S <screen_name>
We have created two screens within a shell, and we have given them individual names. Here is what it looks like:
[[email protected] ~]$ screen -ls There are screens on: 12431.update_product_list (Detached) 12378.long_download (Detached) 2 Sockets in /var/run/screen/S-user.
To receive the list of screens, please use the "-ls" option:
To select the screen, you want to return to please type in:
screen -r <screen_name>
For example, we would like to return to the download screen, thus we are going to type:
screen -r 12378.long_download