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.
Thanks Ahamed 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...
why do we have to always query the status immediately after it has exited?!!
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.