Linux/Unix:bg命令示例

时间:2020-01-09 10:45:54  来源:igfitidea点击:

如何在类似Linux/Unix的系统上在后台运行作业或者脚本?
如何在bash或者ksh或者sh shell的后台运行作业?
作业控制不过是根据需要停止/挂起进程(命令)并继续/继续执行的能力。
这是使用您的操作系统和shell(例如bash/ksh或者POSIX shell)完成的。

bg命令是Linux/Unix shell作业控制的一部分。
该命令可以同时用作内部和外部命令。
它会恢复执行已暂停的进程,就好像它们是由&启动的一样。
使用bg命令重新启动已停止的后台进程。

目的

通过将其作为后台作业运行,可以在当前环境中恢复已暂停的作业。

语法

基本语法如下:

bg jobID bg jobID1 jobID2 ... jobIDN

了解工作编号(jobID)

在Shell中有多种引用作业的方法。
字符"%"引入作业规范。

JobID可以是进程ID(PID)号,也可以使用以下符号组合之一:

  • %Number:使用作业号,例如%1或者%2。
  • %String:使用名称以诸如%commandNameHere或者%ping之类的挂起命令开头的字符串。
  • %+或者%%:指当前作业。
  • %-:指上一个作业。

bg命令示例

在开始使用bg命令之前,您还需要了解另外两个命令(键序列)。

如何找到当前会话中的作业状态?

只需使用以下jobs命令即可列出当前bash/ksh/tcsh shell中的所有活动作业:

$ jobs

或者

$ jobs -l

输出示例:

[1]   6107 Running                 gedit fetch-stock-prices.py &
[2]-  6148 Running                 gnome-calculator &
[3]+  6155 Stopped                 ping theitroad.local

在此示例中,输出三个作业:gedit fetch-stock-prices.py(基于gui的文本编辑器),gnome-calculator和暂挂/停止的on onroad.local命令。

如何在当前会话中暂停或者停止作业?

只需使用CtrlZ快捷键序列或者使用kill命令或者pkill命令即可挂起作业:

kill -s stop PID
kill -s stop jobID
pkill -stop PID

放在一起

在此示例中,您将在前台运行ping命令:

ping www.theitroad.local

要暂停ping命令作业,请按Ctrl-Z键序列。
要列出活动的作业,请执行:

$ jobs -l

jobs命令的输出显示以下已停止的作业:

[3]+  Stopped                 ping theitroad.local

现在,使用作业编号3通过输入以下命令来继续执行ping theitroad.local作业:

bg %3

输出示例:

[3]+ ping theitroad.local &
theitroad@wks05:~$ 64 bytes from www.theitroad.local (75.126.153.206): icmp_req=4 ttl=53 time=264 ms
64 bytes from www.theitroad.local (75.126.153.206): icmp_req=5 ttl=53 time=250 ms
64 bytes from www.theitroad.local (75.126.153.206): icmp_req=6 ttl=53 time=251 ms
64 bytes from www.theitroad.local (75.126.153.206): icmp_req=7 ttl=53 time=251 ms
64 bytes from www.theitroad.local (75.126.153.206): icmp_req=8 ttl=53 time=267 ms

这是另一个示例,该示例运行一个名为update-mutual-fund.sh命令的Unix Shell脚本:

~/scripts/scripts/financial/update-mutual-fund.sh --all --output=html
 
### To stop press CTRL + Z ##
jobs -l
 
#### ~/scripts/scripts/financial/update-mutual-fund.sh has job id # 6 
### Resume job id #6 
bg %6

在此示例中,从终端在后台运行gnome-calculator进行计算:

$ gnome-calculator &

输出示例:

[1] 6517

要停止作业ID 1(或者PID 6517),请执行以下kill命令:

$ kill -s stop %1

或者

$ kill -s stop 6517

输出示例:

[1]+  Stopped                 gnome-calculator

一旦停止,您将无法在gnome-calculator中输入数字。
要恢复已停止的gnome-calculator,请执行:

$ jobs -l
$ bg %1

或者

$ jobs -l
$ bg %gnome-ca

输出示例:

[1]+ gnome-calculator &

bg命令选项

从bash(1)命令手册页或者执行以下命令:

$ help bg

输出示例:

bg: bg [job_spec ...]
    Move jobs to the background.
 
    Place the jobs identified by each JOB_SPEC in the background, as if they
    had been started with `&'.  If JOB_SPEC is not present, the shell's notion
    of the current job is used.
 
    Exit Status:
    Returns success unless job control is not enabled or an error occurs.

关于/usr/bin/bg和shell内置的注释

输入以下" type command"以找出bg是shell,外部命令还是两者的一部分:

$ type -a bg

输出示例:

bg is a shell builtin
bg is /usr/bin/bg

在几乎所有情况下,您都需要使用bg命令,该命令是作为内置的BASH/KSH/POSIX shell实现的。
/usr/bin/bg命令不能在当前会话中使用。
/usr/bin/bg命令在不同的环境中运行,并且不共享父bash/kshs shell对作业的理解。