Bash shell内置wait命令简介
2018-01-18 11:43
771 查看
转自:http://nanxiao.me/bash-shell-wait/
Does bash script wait for one process to finish before executing another?。
Bash shell内置了
wait命令,官方文档对
wait解释如下:
wait wait [-n] [jobspec or pid …] Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If the -n option is supplied, wait waits for any job to terminate and returns its exit status. If neither jobspec nor pid specifies an active child process of the shell, the return status is 127.
wait命令可以使当前
shell进程挂起,等待所指定的由当前
shell产生的子进程退出后,
wait命令才返回。
wait命令的参数可以是进程
ID或是
job specification。举例如下:
root# sleep 10 & [3] 876 root# wait 876 [3]+ Done sleep 10 root# sleep 20 & [1] 877 root# wait %1 [1]+ Done sleep 20
wait命令一个很重要用途就是在
Bash shell的并行编程中,可以在
Bash shell脚本中启动多个后台进程(使用
&),然后调用
wait命令,等待所有后台进程都运行完毕,
Bash shell脚本再继续向下执行。像下面这样:
command1 & command2 & wait
Bash shell还有一个内置变量:
$!,用来记录最后一个被创建的后台进程。
root# sleep 20 & [1] 874 root# sleep 10 & [2] 875 root# echo $! 875
echo $!输出结果是
875,是第二个执行的
sleep命令。参考资料:
Does bash script wait for one process to finish before executing another?。
相关文章推荐
- bash shell内置命令
- mac上的终端bash命令(一) Bourne-Again Shell简介\mac上的终端bash命令(二)基础
- Linux基础学习之bash shell内置命令----type
- bash 内置wait命令使用方法
- 列出所有Bash Shell内置命令的方法示例
- Linux 基础知识:Bash Shell 内置命令
- BASH命令和SHELL脚本总结(16)父脚本中export的变量在子脚本中的可见性测试
- 关于linux bash shell中sort命令指定分隔符的用法
- 重要UNIX命令:C Shell内置命令
- Bash内置命令
- shell 常用命令语法简介
- Shell内置调试命令
- Linux shell 简介1——命令的别名、Shell 函数/从这里出发/Shell 常见问题
- 第四章 更多bash shell命令二
- 【Bash百宝箱】shell内建命令之hash
- 【Shell常用命令一】echo bash alias history 输出重定向 快捷键
- Shell简介:什么是Shell,Shell命令的两种执行方式
- Shell简介:什么是Shell,Shell命令的两种执行方式
- 【Bash百宝箱】shell历史命令(history)
- shell简单使用(四)内置命令