linux运行外壳代码
Under some situation, we want to start some long jobs and then return to the shell to continue our work. We can do this by starting a job in the background. The shell such as bash will keep a list of background jobs and their status. But only one foreground job is allowed. Job control is one of the functions of all the modern shells. In this post, we are using bash.
在某些情况下,我们想开始一些漫长的工作,然后返回外壳继续我们的工作。 我们可以通过在后台启动作业来做到这一点。 诸如bash之类的shell将保留后台作业及其状态的列表。 但是只允许一项前台工作。 作业控制是所有现代Shell的功能之一。 在这篇文章中,我们正在使用bash。
Start a background job and return to the shell:
开始后台作业并返回到shell:
command &
The “&” symbol tells the shell to run this “command” in background. The use are return to the shell prompt immediately without waiting for the job to end. If the “&” symbol is not used, the user must wait for the command to end to get the shell prompt.
“&”符号告诉外壳程序在后台运行此“命令”。 用途是立即返回到shell提示符,而无需等待作业结束。 如果未使用“&”符号,则用户必须等待命令结束以获取shell提示。
Another situation is that the user forget to start a long job with a “&” in the end and so the job is running in the foreground. The job can also be changed to the background by two steps:
另一种情况是用户忘记以“&”结尾开始长时间工作,因此该工作在前台运行。 也可以通过两个步骤将作业更改为后台:
First, suspend the foreground job:
首先,暂停前台作业:
Ctrl + Z
Second, continue the job in background:
其次,在后台继续工作:
bg
List the background jobs and their status:
列出后台作业及其状态:
The background jobs and their status can be queried by the build-in command jobs
可以通过内置命令作业查询后台作业及其状态
jobs
The result is a list of lines. Each line starts with a number. This number can be used to control the jobs.
结果是行列表。 每行以一个数字开头。 该数字可用于控制作业。
Bring a job to foreground and background:
将工作带到前台和后台:
Bring a suspended job to background has been introduced before, the format is:
以前已经介绍过将挂起的作业带到后台,其格式为:
bg %n
n is the number of the job that can be got from the job command.
n是可以从job命令获得的作业编号 。
Bring a background job the foreground:
使后台作业成为前台:
fg %n
Kill a background job:
杀死后台工作:
We can use the format “kill pid” to kill a process. But we can also use the job number to kill the background jobs like this:
我们可以使用“ kill pid”格式来杀死进程。 但是我们也可以使用作业号杀死这样的后台作业:
kill %n
n is the job number.
n是工作编号。
翻译自: https://www.systutorials.com/start-a-job-and-return-to-the-shell-run-a-backgroud-job/
linux运行外壳代码