Skip navigation

我们用“&”把进程放入后台以后,如果需要了解进程的执行情况,可以使用wait函数。默认情况下wait会等待任意子进程结束但是不会返回子进程的返回值。而以子进程的pid作为参数调用wait时,wait便能够返回该子进程的退出状态了。具体操作如下:

#!/bin/bash
command1 &
command2 &
command3 &
for pid in $(jobs -p)
do
  wait $pid
  [ "x$?" == "x0" ] && ((count++))
done

这里我们借助了“jobs -p“来获得所有后台进程的pid。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>