简体   繁体   English

命令以捕获bash中最后一个后台进程的退出状态

[英]command to capture exit status of last background process in bash

i need exit status of last background task completed.so that i can judge if it is failed or passed. 我需要完成上一个后台任务的退出状态,以便我可以判断它是否失败或通过。 I do this . 我做这个。

#!bin/sh
task1 &
task2

echo $? # give the exit status of last command
  1. how to get the exit status of task1. 如何获取task1的退出状态。
  2. do i need to keep wait command after task2 ? 我需要在task2之后保留wait命令吗?

You can't get the exit status of the background command unless you call wait to wait for it to finish. 除非调用wait等待它完成,否则您将无法获得后台命令的退出状态。 The exit code from wait is the exit code of the background task, so if you check wait 's exit code that'll be the value you want. wait的退出代码是后台任务的退出代码,因此,如果选中wait的退出代码,它将是您想要的值。

task1 &
task2

wait    
echo $?

Wait does not return the status of task1. 等待不会返回task1的状态。 If you need the status you will have to specify the pid of the task1. 如果您需要状态,则必须指定task1的pid。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM