LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Capture error before backgrounding? (https://www.linuxquestions.org/questions/linux-newbie-8/capture-error-before-backgrounding-629733/)

enigmaedge 03-21-2008 04:06 PM

Capture error before backgrounding?
 
Hey guys,

First, I apologize if this is the wrong forum to ask my question but I'm new here.

Here's my question:

I'm writing a bash script to execute ./MyCommand then continue to do other things. Specifically, I'm trying to capture the errno of ./MyCommand in order to gracefully clean up any preprocessing I've done.

So far I've come up with this:
Code:

# Some preprocessing is done
...

./MyCommand

if [ $? -ne 0 ]; then
echo "Error in execution.  Cleaning up!"
# cleanup function
do_cleanup
exit 1
fi
# Never gets here =(

The obvious problem is that ./MyCommand is an executable that keeps running and I would like to put it in the background so to continue doing other things passed this part. The above only works when it fails, yet hangs if it is successful.

Therefore, I've tried this:
Code:

# Some preprocessing is done
...

# Backgrounding my process
./MyCommand &

if [ $? -ne 0 ]; then
echo "Error in execution.  Cleaning up!"
# cleanup function
do_cleanup
exit 1
fi
# Always gets here, even when ./MyCommand fails =(

But since the backgrounding itself is always succesful whether or not the actual command is successful, this gives me a false positive.

This comes to my question: How can I merge these two methods? I'd like to background my process, while at the same time capture the error of the actual command so I can do the appropriate clean up?

Thanks in advance!

bigrigdriver 03-22-2008 09:17 AM

See the Advanced Bash-Scripting Guide, Chapter 23. Functions, for discussion on where in the script to declare a function, and how to call it.
Code:

function declaration before the call

# Some preprocessing is done
...

./MyCommand

if [ $? -ne 0 ]; then
echo "Error in execution.  Cleaning up!"
# cleanup function
do_cleanup()          # note the parentheses in the function call
exit 1
fi

It might be a good idea to give this part of the code a graceful exit.
Code:

# Some preprocessing is done
...

./MyCommand

if [ $? -ne 0 ]; then
echo "Error in execution.  Cleaning up!"
# cleanup function
do_cleanup
exit 1
elif [ $? -eq 0 ]
echo "Scripte completed without errors."
exit 0
fi


makyo 03-22-2008 01:38 PM

Hi.

The bash built-in wait will wait for a process to complete and report the status. One captures the process ID of the most recent process placed in the background with $!
Code:

#!/bin/bash -

# @(#) s1      Demonstrate capture and test of exit status of background process.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)

./s0 &
myid=$!

echo
echo " Debug - process id is $myid"

wait $myid
status=$?
if [ $status = 0 ]
then
  echo " Everything is OK - status $status."
else
  echo " Process $myid: abnormal termination - status $status."
fi

exit 0

Producing (with s0 returning non-zero exit status):
Code:

$ ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
 (s0 running, will abort.)

 Debug - process id is 662
 Process 662: abnormal termination - status 1.

So before the wait, you can do unrelated processing.

See bash's documentation on this: help wait ... cheers, makyo

prad77 03-22-2008 03:57 PM

I wonder why you bothered to background the job, since the next step is to ask if it worked. Why not just leave it in the foreground? Well, maybe it was just an over-simplified example!!

Fedora Development


All times are GMT -5. The time now is 08:59 PM.