how to exit status of a piped process ???

Hi,

I've searched the related threads both in this forum and others in google and found the solution to be working too in most of the places. But somehow it's not working for me.

$cmd | tee -a $LOGFILE &

pid=$!
wait ${pid}
ret=$?

echo "$ret"

I want the exit status of the $cmd. but I found that even when this $cmd fails, it echoes 0 as the return status.:wall:

My doubt is:
Is there a possibility that in my code, it's echoing 0 because the command after the pipe is successful? If so, how do i get the exit status of $cmd?

My requirement is that the process HAS TO BE background and also if it exits with error, I should be able to get its proper exit status.This particular code seem to have worked for many as i stated before.:confused:

I'm using bash, and the linux distro is RHEL.

I appreciate any help in this regard.

Yes, it is because "tee" is successful.
You can try this...

$cmd >> $LOGFILE &  
pid=$! 
wait ${pid} 
ret=$?  
echo "$ret"

--ahamed

1 Like

Thanks Ahamed :slight_smile: But i want the output of $cmd to be printed on both console and to log-file together!

I wonder if this make sense: having a command that that can take PID as input and give us the exit status if it has already exited and throw an error if it has not yet exited!!

of course i'm not sure how easy or difficult this is...:confused:

why do we have to always query the status immediately after it has exited?!!

problem not solved yet!!:frowning:

bash$ (echo abc; exit 1) |(wc;exit 2)
      1       1       4
bash$ echo ${PIPESTATUS[0]}
1
1 Like

Thanks binlib. Yes I'm aware of the PIPESTATUS array. But it works only with piped foreground processes and not background ones.

I want the processes to run in background because I want them to be interrupted when a user presses ctrl+c... if it runs in foreground, it'll get the higher priority and won't stop until it finishes even though user has interrupted it.

This is the reason for wanting it to run in background.

Perhaps something along these lines would be sufficient:

($cmd; echo $? > cmd_status) | tee -a $LOGFILE &

pid=$!
wait ${pid}
ret=$(cat cmd_status)

echo "$ret"

Regards,
Alister

1 Like

Hi,
Thanks Alister. I tried this. but it echoes blank.

 I tried with simple commands at prompt and echoed $cmd_status like:
wc -k; echo $? > cmd_status

prints blank

(wc -k; echo $? > cmd_status) 

prints blank

wc -k;cmd_status=$?

prints proper exit status (but this is not useful as it would not serve my purpose if i pipe after this :()

(wc -k;cmd_status=$?)

prints blank

and yes i took care to clear $cmd_status after each try !
Strange!

:wall:

Storing $? in a subshell variable will not work as that variable will not be accessible from the parent shell.

The example I posted works.

You say for you it prints blank, but I see nothing in your attempts that actually reads the file storing the status.

Regards,
Alister

1 Like

Oh! I totally forgot to replace the echo statement with cat.
Thanks a lot to all of u. :slight_smile: