kiran bobbu
kiran bobbu

Reputation: 143

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.
  2. do i need to keep wait command after task2 ?

Upvotes: 2

Views: 2813

Answers (2)

ShivaPahwa
ShivaPahwa

Reputation: 53

Wait does not return the status of task1. If you need the status you will have to specify the pid of the task1.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 362087

You can't get the exit status of the background command unless you call wait to wait for it to finish. 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.

task1 &
task2

wait    
echo $?

Upvotes: 3

Related Questions