how to exit status of a piped process ???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to exit status of a piped process ???
# 1  
Old 09-10-2011
Lightbulb 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.

Code:
$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.Smilie

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.Smilie

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

I appreciate any help in this regard.
# 2  
Old 09-10-2011
Yes, it is because "tee" is successful.
You can try this...

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

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 09-10-2011
Thanks Ahamed Smilie 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...Smilie

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

problem not solved yet!!Smilie
# 4  
Old 09-10-2011
Code:
bash$ (echo abc; exit 1) |(wc;exit 2)
      1       1       4
bash$ echo ${PIPESTATUS[0]}
1

This User Gave Thanks to binlib For This Post:
# 5  
Old 09-10-2011
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.
# 6  
Old 09-11-2011
Quote:
Originally Posted by ashwini.engr07
Code:
$cmd | tee -a $LOGFILE &

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

echo "$ret"

Perhaps something along these lines would be sufficient:
Code:
($cmd; echo $? > cmd_status) | tee -a $LOGFILE &

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

echo "$ret"

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 09-11-2011
Hi,
Thanks Alister. I tried this. but it echoes blank.

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

prints blank
Code:
(wc -k; echo $? > cmd_status)

prints blank
Code:
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 Smilie)
Code:
(wc -k;cmd_status=$?)

prints blank

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

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

2. Shell Programming and Scripting

SSH Process monitoring and Exit Status evaluation

Hi All, I have a peculiar requirement as follows, I have a some hosts on which i have to run a script, so i am using the following code piece for i in $HOSTLIST do ssh ${i} "~/task.sh" done Now i want to run this same thing in parallel on all the hosts and then monitor the ssh process... (1 Reply)
Discussion started by: mihirvora16
1 Replies

3. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

4. Shell Programming and Scripting

Unable to find exit status of piped command

Lois_Answer_Code=`sipsak -vv -s sip:192.168.1.3|grep -A 1 "reply received after"|grep SIP|awk '{print $2}'`How to find the exit status of | (12 Replies)
Discussion started by: proactiveaditya
12 Replies

5. Shell Programming and Scripting

Exit status redirection

Hi, I'm having this simple code below, the file serverlist has a list of IPs one per line. When executed the while loop is executed only once, after that the program terminates. How should i redirect the exit status, so that the entire list of IP will get executed? #!/bin/bash exec <... (4 Replies)
Discussion started by: agent001
4 Replies

6. Shell Programming and Scripting

Exit status of grep

I am trying to get the exit status of grep and test a condition with it, But it does not seem to be working as expected since i am doing something wrong apparently as per grep help Exit status is 0 if match, 1 if no match, and 2 if trouble. My problem is something like this templine - a... (7 Replies)
Discussion started by: prasbala
7 Replies

7. Shell Programming and Scripting

Exit status

I'm preparing for exam and one of exams is to write own test command... I wonder if in unix is a command which just returns exit code you specify.. I know I can easily write a function like this: exStatus() { return $1 } -> my question is rather theoretical thank you! (9 Replies)
Discussion started by: MartyIX
9 Replies

8. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

9. Shell Programming and Scripting

exit status for isql

I'm trying to write a script that will update a table in sysbase. If it's failed then I want to rerun it one more time before exiting the script (fail due to bad value such as trying to put a string into datetime field or bad connection to the database) Well my code below will always return... (2 Replies)
Discussion started by: sirrtuan
2 Replies

10. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies
Login or Register to Ask a Question