LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2008, 04:06 PM   #1
enigmaedge
LQ Newbie
 
Registered: Mar 2008
Posts: 1

Rep: Reputation: 0
Question 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!
 
Old 03-22-2008, 09:17 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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
 
Old 03-22-2008, 01:38 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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
 
Old 03-22-2008, 03:57 PM   #4
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
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

Last edited by prad77; 04-17-2008 at 03:22 AM.
 
  


Reply

Tags
bash, errno



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
no IP address yet... backgrounding. bonito SUSE / openSUSE 3 08-10-2005 11:53 PM
Mount: Backgrounding mijohnst Linux - Networking 0 06-06-2005 12:06 PM
Backgrounding Lan connection Carroarmato0 Linux - Networking 2 03-11-2005 02:48 PM
DHCP and IP backgrounding titus_a_duxass Linux - Wireless Networking 2 02-20-2005 05:00 AM
no ip adress yet.... backgrounding??? ronyx Linux - Networking 2 10-03-2004 07:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration