以下某些作业控制命令接受作业标识符作为参数。 请参阅本章末尾的表格。
列出在后台运行的作业,并给出作业号。 不如 ps 命令有用。
从 shell 的活动作业表中移除作业。
fg 命令将后台运行的作业切换到前台。 bg 命令重新启动挂起的作业,并在后台运行。 如果未指定作业号,则 fg 或 bg 命令作用于当前正在运行的作业。
暂停脚本执行,直到后台运行的所有作业都终止,或者直到指定为选项的作业号或进程 ID 终止。 返回等待命令的退出状态。
您可以使用 wait 命令来防止脚本在后台作业完成执行之前退出(这将创建一个可怕的孤儿进程)。
示例 15-26. 等待进程完成再继续
#!/bin/bash ROOT_UID=0 # Only users with $UID 0 have root privileges. E_NOTROOT=65 E_NOPARAMS=66 if [ "$UID" -ne "$ROOT_UID" ] then echo "Must be root to run this script." # "Run along kid, it's past your bedtime." exit $E_NOTROOT fi if [ -z "$1" ] then echo "Usage: `basename $0` find-string" exit $E_NOPARAMS fi echo "Updating 'locate' database..." echo "This may take a while." updatedb /usr & # Must be run as root. wait # Don't run the rest of the script until 'updatedb' finished. # You want the the database updated before looking up the file name. locate $1 # Without the 'wait' command, in the worse case scenario, #+ the script would exit while 'updatedb' was still running, #+ leaving it as an orphan process. exit 0 |
可选地,wait 可以接受作业标识符作为参数,例如,wait%1或wait $PPID. [1] 请参阅作业 ID 表。
![]() | 在脚本中,使用 & 在后台运行命令可能会导致脚本挂起,直到按下 ENTER 键。 这似乎发生在写入stdout的命令中。 这可能是一个主要的烦恼。
在后台命令后放置 wait 似乎可以解决这个问题。
|
这具有类似于 Control-Z 的效果,但它会挂起 shell(shell 的父进程应在适当的时候恢复它)。
退出登录 shell,可以选择指定退出状态。
给出执行命令时系统经过时间的统计信息,格式如下
0m0.020s 0m0.020s |
此功能价值相对有限,因为对 shell 脚本进行性能分析和基准测试并不常见。
通过向进程发送适当的终止信号来强制终止进程(请参阅示例 17-6)。
示例 15-27. 一个杀死自身的脚本
#!/bin/bash # self-destruct.sh kill $$ # Script kills its own process here. # Recall that "$$" is the script's PID. echo "This line will not echo." # Instead, the shell sends a "Terminated" message to stdout. exit 0 # Normal exit? No! # After this script terminates prematurely, #+ what exit status does it return? # # sh self-destruct.sh # echo $? # 143 # # 143 = 128 + 15 # TERM signal |
killall 命令通过名称而不是进程 ID 杀死正在运行的进程。 如果有特定命令的多个实例正在运行,则对该命令执行 killall 将终止所有实例。
![]() | 这是指/usr/bin中的 killall 命令,而不是/etc/rc.d/init.d. |
bash$ command ls |
![]() | command 指令禁用紧随其后的命令的别名和函数。 |
enable这启用或禁用 shell 内建命令。 例如,enable -n kill禁用 shell 内建命令 kill,以便当 Bash 随后遇到 kill 时,它会调用外部命令.
这是 ksh 自动加载器的 Bash 端口。 使用 autoload,具有 autoload 声明的函数将在首次调用时从外部文件加载。 [3] 这节省了系统资源。请注意,autoload 不是核心 Bash 安装的一部分。 它需要使用enable -f
[1] | 注释 |
[2] | 这当然仅适用于子进程。许多可加载的内建命令的 C 源代码通常可以在/usr/share/doc/bash-?.??/functions 目录中找到。请注意,enable 的-f |
[3] | 选项并非可移植到所有系统。 |