linux退出后还有bash进程,在BASH中获取后台进程的退出状态

PIDS+=$!

…不是你想的那样。考虑:

PIDS=( )

PIDS+=11

PIDS+=22

PIDS+=33

declare -p PIDS

…如果你

期待

declare -a PIDS='([0]="11" [1]="22" [2]="33")

……你会错的,因为

发射是:

declare -a PIDS='([0]="112233")'

+=

只有当右边的东西是数组时,才追加一个新的数组元素。

因此,你得到一个

not a child of this shell

错误,因为将所有PID连接到一个字符串中的结果并不是一个实际存在的PID。

PIDS+=( "$!" )

提供端到端示例:

#!/usr/bin/env bash

# run four different processes; two exit status 0, one exits status 1, on exits status 2

# ...exits happen at delays ranging between 2-5 seconds.

delays=( 5 3 2 4 )

exits=( 0 0 1 2 )

for idx in "${!delays[@]}"; do

(sleep "${delays[$idx]}"; exit "${exits[$idx]}") &

pids+=( "$!" )

done

exit_status=0

for pid in "${pids[@]}"; do

wait "$pid"; (( exit_status |= $? ))

done

echo "Combined exit status is $exit_status"

exit "$exit_status"

Combined exit status is 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值