SEARCH

How-To Geek

How to Multitask in the Linux Terminal: 3 Ways to Use Multiple Shells at Once

image

The Linux terminal has rich multitasking capabilities. You can switch between the virtual consoles already running on your system, use Bash job control to run processes in the background, and take advantage of GNU screen, a terminal “window manager.”

You don’t have to stick to a single command at a time. Whether you want to run a process in the background and revisit it occasionally or run multiple time-consuming tasks at once, Linux offers several options.

Virtual Consoles

By default, most Linux systems have several virtual consoles running in the background. Switch between them by pressing Ctrl-Alt and hitting a key between F1 and F6. Ctrl-Alt-F7 will usually take you back to the graphical X server.

Pressing the key combination will take you to a login prompt. You can log in and run a command, then switch away – the command will continue running in the background, so you can have several different terminal sessions going at the same time.

image

The “tty1” text here indicates that this is the terminal located at Ctrl-Alt-F1, tty2 would be at F2.

These are just the most common settings – different Linux distributions could have less virtual consoles running and have the X server located at a different place.

Bash Job Control

The Bash shell provides its own features for handling multiple processes. Job control lets you run processes and attach and detach from them. An attached process is known as a foreground process, while a detached one is known as a background process.

To start a process in the background, add the & character after its command. For example, to open the Nano text editor as a background job, type the following command:

nano &

image

The [1] indicates that our new job’s job ID is 1. The 3751 is its process ID.

Every job we start gets its own job ID. Run the jobs command to view the list of currently running jobs. The + sign indicates the job that was last associated with the shell, while the – sign indicates the job that was second-to-last associated with the shell.

image

The fg command allows you to bring a background job to the foreground, attaching it to the current shell. Running fg or another job-related command without specifying a job will use the last-associated job – the one with a + sign in the jobs list. You can also specify a job number. For example, the following command will bring job 1 back to the foreground:

fg %1

image

End a process normally and it will vanish from the list of running jobs. If you want to detach a job from the current shell, use the ^Z – that is, Ctrl-Z – keyboard shortcut.

image

GNU Screen

GNU Screen is a “full-screen window manager” that lets you use multiple shells in a single terminal. It may not be installed by default – it isn’t on Ubuntu. On Ubuntu or Debian, use the following command to install Screen:

sudo apt-get install screen

Once it’s installed, run the screen command and you’ll see some information about Screen.

image

Press Space or Enter and you’ll see a normal-looking terminal.

image

It may not look special, but this shell is actually running within GNU Screen. Press Ctrl-a and then d to detach from Screen. You’ll be back to the normal terminal.

image

To reattach to screen, run the screen -r command. You’ll be back to the same terminal as before.

image

There’s a lot more you can do with screen. For example, create a new “window” (terminal) in screen by pressing Ctrl-a, then c. Once you have multiple windows, press Ctrl-a twice to switch between them. You can also press Ctrl-a, then to see a list of windows.

image

Select a window in the list and press Enter to switch to it.


We’ve previously written about using GNU Screen and gone over using Byobu, which enhances GNU Screen.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Google+.

  • Published 04/16/12

Comments (10)

  1. rocky

    Good to know about the How to Multitask in the Linux Terminal: 3 Ways to Use Multiple Shells at Once

  2. mnicky

    And what about tmux?

  3. arukaen

    or terminator

  4. joker belmont

    How do you capture screen from bash?

  5. oktboer

    yep, don’t forget about terminator and byobu (pimped screen), both my favorites.

  6. Dorian Patterson

    Tmux is a superior terminal multiplexer than screen. If you can use tmux, don’t use screen. If you can use terminator though, use that instead.

  7. gtcodave

    regarding previous comments…

    tmux has incompatibility issues, or did you not know?
    terminator, whilst an interesting option for people who like windows. style desktop environments, isn’t a great option for people who like tiling window managers or headless servers.

    so for those of you hunting, screen is great and widely available, other wise the less flexible bash :)

    glhf

  8. Trae

    Am I right in saying that if you use Bash Job Control, even running something in the background, everything ends when that bash session closes?

    In other words, if you ssh’d into a server, ran “nano &” then disconnected from your ssh session, nano would end as well? Screen allows you to multitask without the fear of loosing long processes (rsync?) if you get disconnected.

  9. Sudo Bash

    @Trae
    The best way to make sure that background jobs are not killed is by closing the terminal is by typing the command ‘disown.’ The commands take all of the processes listed under the terminals jobs and disassociates them with the terminal.This is useful when you want to start an application with the terminal.

    Some other tips:

    Typing just ‘%’ will default to the last started job
    If you want to kill a misbehaving background job, type kill % or just kill % to kill the last started job
    If you forget to put a job in the background you can still do it without restarting it. Stop it and use the bg command to put it in the background. example:
    $firefox
    ^Z
    $bg

    If you stop a job with ^Z you can continue it in the foreground by typing ‘fg’.

  10. scottreddy

    hi everyone how do you clean up the system and finding out how to run more monitors with the system i have 3 of them but only the one one the motherboard is working thank you

Enter Your Email Here to Get Access for Free:

Go check your email!