LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-20-2009, 01:54 PM   #1
e1ectthedead
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
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?
 
Old 12-20-2009, 04:42 PM   #2
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Quote:
Originally Posted by e1ectthedead View Post
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
 
Old 12-20-2009, 11:54 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 12-21-2009, 08:32 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,282

Rep: Reputation: 1088Reputation: 1088Reputation: 1088Reputation: 1088Reputation: 1088Reputation: 1088Reputation: 1088Reputation: 1088
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."
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
What if 'kill -9' fails to kill a task? chii-chan Linux - Newbie 15 03-27-2013 03:47 PM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 09:42 AM
Hacking BASH source code assyrian1 Programming 8 04-20-2006 07:02 PM
hacking BASH source code assyrian1 Linux - Newbie 1 04-19-2006 07:36 PM
where is the bash source code?... gearoid Programming 5 11-25-2003 04:24 AM


All times are GMT -5. The time now is 06:51 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration