LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 10-13-2012, 02:00 PM   #1
mariano1
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
BASH - Stop execution of sourced scripts


I have a script that runs other scripts with source. What i want to do is to find those scripts being run, and if any of them is running, then stop executing it. Something like this:

Code:
 #!/bin/bash

 source /path/script1.sh
 source /path/sript2.sh
 source /path/sript3.sh

 if [ a_script_is_running ]
 then
      stop_execution scriptn.sh
 fi
If one of those scripts was running an infinite loop, is there a way to stop this loop and to continue with the execution of the main script?
 
Old 10-13-2012, 02:15 PM   #2
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,413

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
Your example won't work because it won't reach the if statement until script1-3 are finished executing. Script2 won't run until 1 is finished, 3 won't run until 2 is finished, and the if statement won't run until 3 is finished. So if any of them is stuck in an infinite loop, everything will be stuck.

If these were programs you were calling, you could run them in the background and monitor their progress, but that won't work for sourcing. I would look into script1-3 individually and fix the infinite loop. What you're proposing is an external bandaid to a problem that should be fixed internally.
 
1 members found this post helpful.
Old 10-13-2012, 02:33 PM   #3
mariano1
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
The problem is that those scripts are given to me and I cannot change them
 
Old 10-13-2012, 02:52 PM   #4
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,413

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
Do these scripts have to be sourced? In other words, do they rely on a shared environment, or can they be executed independently in their own sub-shells?
 
Old 10-13-2012, 02:55 PM   #5
mariano1
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unfortunately they share variables with the main script, so yes, I need to source them
 
Old 10-13-2012, 03:06 PM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,413

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
Then I don't think there's much you can do. I would start by contacting the person who gave you the scripts and talk to them about fixing the problem. Do you know why they get stuck in an infinite loop?
 
Old 10-13-2012, 06:46 PM   #7
mariano1
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for your answer, I contacted the person who is suppose to give me those scripts and it'll never happen something like that, so I don't have to worry about it
 
Old 10-14-2012, 08:17 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
I think it's possible. You can run the sourced script in a subshell, and fork that subshell to the background.

sourcescript.sh
Code:
while (( i++ <= 10 )); do
        echo "foo"
        sleep 2
done
mainscript.sh
Code:
#!/bin/bash

echo "starting sourcescript.sh"

( . sourcescript.sh ) &
pid=$!

sleep 10

echo "stopping sourcescript.sh"
kill "$pid"

exit 0
It's still a band-aid though, as suicidaleggroll said. It's much better to fix the problem at the source.
 
Old 10-15-2012, 06:55 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 650

Rep: Reputation: 268Reputation: 268Reputation: 268
Quote:
Originally Posted by David the H. View Post
I think it's possible. You can run the sourced script in a subshell, and fork that subshell to the background.
But then any changes to the environment made by the script in the subshell won't take effect in the environment of the "parent" script. Actually, this kind of takes away the point of sourcing the scripts in the first place and does not differ much from just running those scripts independently as suggested by suicidaleggroll earlier.
 
Old 10-15-2012, 07:53 AM   #10
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,001

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
also, you cannot kill a sourced script, this kill will stop the "parent" shell also (because it is not a real parent, but the same process).
From the other hand you may try to start another task and kill this process in case you want to stop it completely.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash; terminate sourced script without closing its shell porphyry5 Programming 16 07-13-2012 12:39 PM
control the execution of the scripts prravin1 Programming 12 03-06-2012 05:47 AM
stop scripts execution using apache asim.mcp Linux - Server 1 04-17-2011 11:08 AM
how to stop execution of a program if it is already running? kkpal Linux - Newbie 8 07-23-2008 12:52 PM
Parallel Execution Capable Scripts? irfanhab Programming 5 08-28-2004 06:04 AM


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