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.
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 &
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.
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
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.
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 screenOnce it’s installed, run the screen command and you’ll see some information about Screen.
Press Space or Enter and you’ll see a normal-looking terminal.
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.
To reattach to screen, run the screen -r command. You’ll be back to the same terminal as before.
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.
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.
No comments:
Post a Comment