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.”

Linux终端具有丰富的多任务处理功能。 您可以在系统上已经运行的虚拟控制台之间进行切换,使用Bash作业控件在后台运行进程,并利用GNU屏幕(终端“窗口管理器”)的优势。

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.

您不必一次只遵循一个命令。 无论您是要在后台运行某个进程并偶尔重新访问它还是一次运行多个耗时的任务,Linux都提供了多种选择。

虚拟控制台 (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.

默认情况下,大多数Linux系统在后台运行多个虚拟控制台。 通过按Ctrl-Alt并按F1F6之间的键在它们之间切换。 Ctrl-Alt-F7通常会将您带回到图形X服务器。

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.

此处的“ tty1”文本表示这是位于Ctrl-Alt-F1的终端,而tty2将位于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.

这些只是最常见的设置-不同的Linux发行版可能运行较少的虚拟控制台,并且X服务器位于不同的位置。

重击作业控制 (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.

Bash shell提供了自己的功能来处理多个进程。 作业控制使您可以运行流程以及将其附加和分离。 附加进程称为前台进程,而分离进程称为后台进程。

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文本编辑器作为后台作业打开,请键入以下命令:

nano &

纳米&

image

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

[1]表示我们新作业的作业ID为1。3751是其进程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.

我们开始的每个工作都有其自己的工作ID。 运行作业命令以查看当前正在运行的作业列表。 +号表示与外壳最后关联的作业,而–号表示与外壳倒数第二个关联的作业。

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命令允许您将后台作业带到前台,并将其附加到当前shell。 在未指定作业的情况下运行fg或其他与作业有关的命令将使用最后关联的作业-作业列表中带有+号的作业。 您也可以指定工作编号。 例如,以下命令将作业1带回到前台:

fg %1

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.

正常结束进程,它将从正在运行的作业列表中消失。 如果要从当前外壳分离作业,请使用^ Z (即Ctrl-Z )键盘快捷键。

image

GNU屏幕 (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:

GNU Screen是一个“全屏窗口管理器”,可让您在单个终端中使用多个shell。 默认情况下可能未安装它-Ubuntu上没有。 在Ubuntu或Debian上,使用以下命令安装Screen:

sudo apt-get install screen

sudo apt-get安装屏幕

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

安装完成后,运行screen命令,您将看到有关Screen的一些信息。

image

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

按空格键或Enter键,您将看到外观正常的终端。

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.

它可能看起来并不特殊,但是此shell实际上在GNU Screen中运行。 按Ctrl-a ,然后按d从屏幕分离。 您将回到正常的终端。

image

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

要重新连接到屏幕,请运行screen -r命令。 您将回到以前的终端。

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.

屏幕可以做很多事情。 例如,通过按Ctrl-a ,然后按c在屏幕上创建一个新的“窗口”(终端)。 拥有多个窗口后,请按Ctrl-a两次以在它们之间切换。 您也可以按Ctrl-a ,然后按以查看窗口列表。

image

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

在列表中选择一个窗口,然后按Enter切换到该窗口。



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

我们之前已经写过有关使用GNU Screen的文章,而不再使用Byobu ,它增强了GNU Screen的功能。

翻译自: https://www.howtogeek.com/111417/how-to-multitask-in-the-linux-terminal-3-ways-to-use-multiple-shells-at-once/

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:1 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐