-   Programming (
-   -   BASH - Stop execution of sourced scripts (

mariano1 10-13-2012 03:00 PM

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:



 source /path/
 source /path/
 source /path/

 if [ a_script_is_running ]

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?

suicidaleggroll 10-13-2012 03:15 PM

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.

mariano1 10-13-2012 03:33 PM

The problem is that those scripts are given to me and I cannot change them

suicidaleggroll 10-13-2012 03:52 PM

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?

mariano1 10-13-2012 03:55 PM

Unfortunately they share variables with the main script, so yes, I need to source them

suicidaleggroll 10-13-2012 04:06 PM

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?

mariano1 10-13-2012 07:46 PM

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

David the H. 10-14-2012 09:17 AM

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

while (( i++ <= 10 )); do
        echo "foo"
        sleep 2


echo "starting"

( . ) &

sleep 10

echo "stopping"
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.

millgates 10-15-2012 07:55 AM


Originally Posted by David the H. (Post 4805339)
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.

pan64 10-15-2012 08:53 AM

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.

All times are GMT -5. The time now is 10:50 AM.