LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-20-2008, 07:43 AM   #1
pradyumna_reddy
LQ Newbie
 
Registered: Nov 2008
Location: Hyderabad
Posts: 2

Rep: Reputation: 0
Unhappy running 2 scripts in background ,if any 1 exits with error the other 1 also exit


Hello,

i am running a Script main.sh which runs 2 scripts in background if one script exits with error another script should also exit.

this is how the scripts look like

Main.sh


#!/bin/bash

cd $YVIEW/

date >ybuild_cmn.log

ybuild -cmn >>ybuild_cmn.log ; [ $? -ne 0 ] && exit 0



$HOME/file1.sh &
if [ $? -ne 0 ] ; then
exit
fi

$HOME/file2.sh & [ $? -ne 0 ] && exit 0
if [ $? -ne 0 ] ; then
exit
fi

file1.sh

#!/bin/bash

echo running file1

echo iew variable = $YVIEW

echo iew name $YVIEWNAME

cd $YVIEW/nemsw/

ybuild -b bam83xx -mr boot >bam83.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam83xx -mr shared >bam83_sha.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam83xx -mr iomod >bam83_io.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam405ex -mr shared >bam83_ex.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam83xx -mr ctrlr >bam83_ctrl.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam405h -mr shctrlr >bam405h_shctrlr.log ; [ $? -ne 0 ] && exit 1

ybuild -b man8541 -mr ndctrlr >bam4054_ndctrlr.log ; [ $? -ne 0 ] && exit 1

exit 0

file2.sh



#!/bin/bash

echo running file2

echo view variable = $YVIEW

echo view name $YVIEWNAME

cd $YVIEW/nemsw/

ybuild -b bam405h -mr boot >bam405h.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam405h -mr shared >bam405h_shared.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam405h -mr iomod >bam405h_iomod.log ; [ $? -ne 0 ] && exit 1

ybuild -b bam405h -mr ctrlr >bam405h_ctrlr.log ; [ $? -ne 0 ] && exit 1

ybuild -b man8541 -mr boot >man8541_bo.log ; [ $? -ne 0 ] && exit 1

ybuild -b man8541 -mr shared >man8541_sh.log ; [ $? -ne 0 ] && exit 1

ybuild -b man8541 -mr shctrlr >man8541_shctrlr.log ; [ $? -ne 0 ] && exit 1

exit 0

but my code is not working as desired ,if any error on in file.sh is not exiting file2.sh also ,in fact it should exit the whole script.

please help me on this
 
Old 11-20-2008, 08:40 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
well your tests are not going to work as you are forking the batch file, so there is no exit code at the time your trying to use it immediately afterwards.


btw, "program || exit 1" is much simpler than "program; [ $? -ne ] && exit 1"
 
Old 11-20-2008, 01:54 PM   #3
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
Why not fork both of them and then have an endless loop at the end of the script that checks the output of 'ps'? If it's not time sensitive, you could have it loop like every minute. Hrmmms but that doesn't check for the exit code it self....

Can you store variables with the output of a forked script?

MYVAR=something.sh &


You could always create a temporary file when all the extra commands exit that the end of the main.sh will check every so often.

this is messy and may require tweaking but...

Code:
file1.sh &
file2.sh &

( somevar=1
  while $somevar
  do
    if [ `cat tempfile` -eq 1 ]; then
      killall file2.sh
      killall file1.sh
    else
       # loop again after so many seconds
       sleep $timedelay
    fi
  done

  # put error messages here

) &

# anything here will be executed immediately.
# You would have to output any error messages from the above code block
I've never tried sending a ( ) code block into the background... it should work.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Log what exits in bash script. What causes exit code thats not 0? Trailsmoke Programming 2 09-25-2008 03:07 AM
How to exit and save (or just to exit) inside a "make config" running command? scrat75 Linux - Kernel 6 09-05-2008 04:02 PM
Shell scripts unable to permanently change directory after exit ernesto_cgf Linux - General 4 12-21-2006 04:08 PM
getting the exit status of a background processes gururaj Linux - General 7 04-27-2006 05:56 PM
Bash - background process and exit ?? michael_util Programming 4 01-05-2005 11:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:34 PM.

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