LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Kill follow-on code if source fails in bash? (https://www.linuxquestions.org/questions/programming-9/kill-follow-on-code-if-source-fails-in-bash-777083/)

e1ectthedead 12-20-2009 01:54 PM

Kill follow-on code if source fails in bash?
 
I have a Bash script that runs other bash scripts. If the parent code fails, is there any way for me to also kill the child code?

For instance, I have this script:

Code:

#!/bin/sh

LCK=`basename $0`.lck

if [ -f "${LCK}" ]; then
  PID =`head -n 1 $LCK`
  if [ -n "ps -p ${PID}`" ] then
    exit
  fi
fi

echo $$ > $LCK

That kills any multiple instances of a script if I run it more than once. Is there any way I can just modify this into something that prevents the child code from running/continuing from running if the parent stops from an error?

GooseYArd 12-20-2009 04:42 PM

Quote:

Originally Posted by e1ectthedead (Post 3799149)
I have a Bash script that runs other bash scripts. If the parent code fails, is there any way for me to also kill the child code?

For instance, I have this script:

Code:

#!/bin/sh

LCK=`basename $0`.lck

if [ -f "${LCK}" ]; then
  PID =`head -n 1 $LCK`
  if [ -n "ps -p ${PID}`" ] then
    exit
  fi
fi

echo $$ > $LCK

That kills any multiple instances of a script if I run it more than once. Is there any way I can just modify this into something that prevents the child code from running/continuing from running if the parent stops from an error?

how is your script starting up its children?

.andy

ta0kira 12-20-2009 11:54 PM

What does this have to do with source? Is that how it runs the other scripts? You might be able to use kill 0, but only if the child processes are "good" and didn't create their own process groups or sessions.
Kevin Barry

sundialsvcs 12-21-2009 08:32 PM

It's easier than you think.

On the command-line:
make depend && make && make install
... will execute each of these three commands in turn, stopping short if any of them do not succeed.

It's not that the following commands are "killed," but rather that they "are not launched."

There is, of course, "more than one way to do it."


All times are GMT -5. The time now is 03:48 PM.