LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2010, 01:07 PM   #1
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 184

Rep: Reputation: 28
Getting return value from program ran in background


I think my title pretty much explains it. I am writing a script and I want to start a program in the background, and when that program finishes I want to check the return value to make sure there was no error.

For example normal I would do something like this:
#!/bin/sh
program
if [ ! $? -eq 0 ]; then
echo "There was an error"
exit 1
fi

Now I want to do something like this:
#!/bin/sh
PRTN=`program1 &`
program2
if [ ! $? -eq 0 ]; then
echo "There was an error in program2"
exit 1
fi
if [ ! $PRTN -eq 0 ]; then
echo "There was an error program1"
exit 1
fi

In this case if program2 finishes before program1, I don't think the return value from program1 $PRTN would be valid at the time it is checked. Any suggestions on how to do something like this?
 
Old 03-10-2010, 06:22 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
program1 &
PID1=$!

if ! program2 ; then
   echo "There was an error in program2"
   exit 1
fi

if ! wait $PID1 ; then
   echo "There was an error in program1"
   exit 1
fi
 
1 members found this post helpful.
Old 03-10-2010, 06:43 PM   #3
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 184

Original Poster
Rep: Reputation: 28
Thanks ntubski. I was going to something like this:

Code:
while [ ! -z "$(ps | grep program1)" ]; do
		sleep 3
done

if [ ! $PRTN -eq 0 ]; then
	echo "There was an error in program1"
        exit 1
fi
Your solution seems a little nicer, so I'll give that a try. Thanks.

Oh and how do the put your code in that code box in your reply?

Last edited by dbrazeau; 03-10-2010 at 07:06 PM.
 
Old 03-10-2010, 07:01 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by dbrazeau View Post
Oh and how do the put your code in that code box in your reply?
Use code tags.


[code]
your code goes here
[/code]
 
  


Reply



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
[SOLVED] Script for cycling desktop background in gnome not working when ran as root figure002 Linux - Desktop 4 08-04-2010 06:59 AM
ran 4 background jobs some have +/- what does that mean? CD_TOASTER Linux - Newbie 1 05-25-2007 04:39 AM
program exit return 0 or 1? blackzone Programming 4 07-20-2006 05:00 AM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
how to check the return value of a program Sammy2ooo Linux - General 1 04-27-2004 02:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 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