当您从 shell(例如firefox
)运行程序时,它将“在前台”执行。当程序完成时,您将有机会执行另一个命令。
另一种执行命令的方法是“在后台”。如果你把这个符号放在&
命令后面,它将异步执行(在后台),你将有可能从同一个 shell/终端执行其他命令。摘自man bash
:
When bash starts a job asynchronously (in the background), it prints a line
that looks like:
[1] 25647
indicating that this job is job number 1 and that the process ID of the
last process in the pipeline associated with this job is 25647.
Run Code Online (Sandbox Code Playgroud)
当你开始第二份工作时,它会回答[2] NewPid
等等。使用内置命令,jobs
您将拥有所有列表。
当您“在前台”运行命令并且想要暂停它(而不是明确停止)时,您可以按CTRL+ Z。外壳会以类似的方式回答你(例如)
[1]+ Stopped firefox
Run Code Online (Sandbox Code Playgroud)
要继续之前的工作,您可以编写%1 &
(与您从终端读取的数字相同)。您也可以使用bg %1
. 它将在后台执行作业 1并返回提示,准备接受新命令。
您可能会发现有趣的文章Linux:在后台启动命令