linux 已退出进程的退出码,linux – 获取后台进程的退出代码

1:在bash,$!保存已执行的最后一个后台进程的PID.无论如何,这将告诉您要监控的流程.

4:等待< n>等待直到具有PID< n>的过程.已完成(它将阻塞直到进程完成,因此您可能不希望在确定进程完成之前调用此方法),然后返回已完成进程的退出代码.

2,3:ps或ps | grep“$!”可以告诉您进程是否仍在运行.由您决定如何理解输出并决定它与完成的接近程度. (ps | grep不是白痴.如果你有时间,你可以想出一个更健壮的方法来判断这个过程是否还在运行).

这是一个骨架脚本:

# simulate a long process that will have an identifiable exit code

(sleep 15 ; /bin/false) &

my_pid=$!

while ps | grep " $my_pid " # might also need | grep -v grep here

do

echo $my_pid is still in the ps output. Must still be running.

sleep 3

done

echo Oh, it looks like the process is done.

wait $my_pid

# The variable $? always holds the exit code of the last command to finish.

# Here it holds the exit code of $my_pid, since wait exits with that code.

my_status=$?

echo The exit status of the process was $my_status

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值