Running Multiple Applications On Linux Using Screen
Modern computer operating systems are designed around the concept of multi-tasking, enabling the computer to run more than one application at any time. Graphical user interfaces make this simple to do, with the ability to layer multiple windows over the top of each other, each running different applications at the same time. The command line presents you – in contrast – with a very stark single task interface as running an application fills the screen.
The screen tool provides for an alternative method of working at the command line. It allows multiple terminal sessions to run simultaneously, and which the user can switch between. This means that multiple applications can run at the same time. Each of the sessions can be run under different users if required, and aren’t directly tied to the user’s logged in session. This means that if the user is logged in over SSH, and for some reason the connection drops, rather than the user’s running application being stopped, it will continue to run in-screen and the user can reconnect to the screen session once they have logged back in. When working with multiple sessions these can either be side by side in a split screen manner similar to using a GUI, or in separate full-screen sessions.
Getting Started with Screen
Screen is often included with your standard Linux server installation, so you shouldn’t need to install it on your system. It can be started with the following command:
screen
The first thing you’ll notice is that this will leave you with a blank command line. This is because screen creates a new terminal session for you to start with.
Working in a shell in screen is much the same as normal, and typing *exit* or pressing ‘Ctrl-d’ will exit the shell closing screen in the process. The real magic of screen comes when you type ‘Ctrl-a’ which is the shortcut to give screen instructions. After this key combination screen will then listen for the next keypress which will be interpreted as an instruction to perform a specific task. The first shortcut combination we’ll look at is ‘Ctrl-a’ followed by ‘?’, which brings up a help screen that shows a number of the keyboard shortcuts and what they do, some of which we’ll cover here.
Keyboard Shortcuts
First, we’ll cover working with multiple views with the split screen view. There are two commands to split the screen, and they will split the current screen section into two parts of equal size. ‘Ctrl-a’ ‘S’ (note the capital S) will split the screen horizontally whilst ‘Ctrl-a’ ‘|’ (the pipe symbol) splits the screen vertically into two side-by-side parts. To move between these multiple screen parts you can use ‘Ctrl-a’ with the ‘tab’ key which will move you to the next section. When the screen is split, a new terminal isn’t automatically launched in the newly created part. To do this you’ll need to navigate to that screen section, then use ‘Ctrl-a’ ‘c’ to create a new terminal window there.
Yes, I said window. Screen uses the concept that you create windows in which you run your commands and then change between those windows which can either be displayed full screen or in parts of the screen once you have split it. So if you are working full screened ‘Ctrl-a’ ‘c’ creates a new window that can be changed to. These windows are numbered with the original window being 0 and the number increasing by 1 for each subsequent window created. Using ‘Ctrl-a’ followed by the number of a window will take you straight to that window. ‘Ctrl-a’ ‘n’ will take you to the next window, and ‘Ctrl-a’ ‘p’ will take you to the previous one.
Copy and Paste
Taking things to the next level, we can copy and paste either on the same window or between windows. Pressing ‘Ctrl-a’ ‘[‘ will enter the copy mode, you can then navigate around, press enter to start copying from the currently highlighted region, navigate around to where you want to end, and then press enter again to copy the section. Pasting can be done with ‘Ctrl-a’ ‘]’.
We can detach from a running screen session using ‘Ctrl-a’ ‘d’ which will put you back at the regular command line, but leaves the screen session and whatever it was doing running in the background. This is perfect for starting a long-running script when you also want to do something else. You can start and detach from as many screen sessions as you like. If you have detached from a single screen session then reattaching is as simple as using the following command:
screen -r
If you have multiple screen sessions that you have detached from, this will issue a warning and give you a list of the sessions that are available. You can then select it by entering the command again followed by the name given which is normally identified by the process id screen was launched as.
So there we have it – a somewhat whistle-stop tour of working with multiple applications at your command line using screen. It should all be fairly straightforward to use, and can definitely empower you when working on a server when you need to do multiple things simultaneously.