How to Multi-Task in Linux with the Command Line
One of the most jarring moments when moving from a Windows-based environment to using the command line is the loss of easy multi-tasking. Even on Linux, if you use an X Window system, you can use the mouse to just click on a new program and open it. On the command line, however, you’re pretty much stuck with what’s on your screen at any given time. In this tutorial, we will show you how to multi-task in Linux with the command line.
Background and Foreground Process Management
However, there are still ways to multi-task in Linux, and some of them are more comprehensive than others. One in-built way that doesn’t require any kind of additional software is simply moving processes into the background and the foreground. We’d written a tutorial on that a short while back. However, it has some disadvantages.
First, to send a process into the background, you have to pause it first. There’s no way to send an already running program into the background and keep it running in one go.
Second, you need to break your workflow to start a new command. You have to exit what you’re currently doing and type more commands into the shell. It works, but it’s inconvenient.
Third, you have to look out for output from the background processes. Any output from them will appear on the command line and interfere with what you’re doing in the current moment. So background tasks need to either redirect their output to a separate file, or they need to be muted altogether.
Because of these disadvantages, there are huge problems with background and foreground process management. A better solution is to use the “screen” command utility as shown below.
But First – You Can Always Open a new SSH Session
Don’t forget that you just open a new SSH session. Here’s a screenshot of we doing just that:
It can get inconvenient to open new sessions all the time. And that’s when you need “screen”
Using “Screen” Instead
The “screen” utility allows you to have multiple workflows open at the same times – the closest analog to “windows”. It’s available by default within the regular Linux repositories. Install it in CentOS/RHEL like this:
sudo yum install screen
Opening a New Screen
Now start your session by typing “screen”.
This will create a blank window within your existing SSH session and give it a number that’s shown in the title bar like this:
My screen here has the number “0” as shown. In this screenshot, I’m using a dummy “read” command to block the terminal and make it wait for input. Now let’s say we want to do something else while we wait.
To open a new screen and do something else, we type:
“ctrl+a” is the default key combination for managing screens within the screen program. What you type after it, determines the action. So for example:
- ctrl+a c – Creates a new screen
- ctrl+a [number] – Goes to a specific screen number
- ctrl+a k – Kills the current screen
- ctrl+a n – Goes to the next screen
- ctrl+a ” – Lists all active screens in the session
So if we press “ctrl+a c”, we get a new screen with a new number as shown here:
You can use the cursor keys to navigate the list and go to whichever screen you want.
Screens are the closest thing you’ll get to a “windows” like system in the Linux command line. Sure, it’s not as easy as clicking with the mouse, but then the graphical subsystem is very resource intensive in the first place. With screens, you can get almost the same functionality and enable full multi-tasking!