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!