mar_sanbas
mar_sanbas

Reputation: 943

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:

 #!/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?

Upvotes: 0

Views: 1599

Answers (3)

cdarke
cdarke

Reputation: 44364

Your basic premise is the problem:

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

                       <- At this point, all those scripts have run to completion

if [ a_script_is_running ]       <- It won't be!
then 
     stop_execution scriptn.sh   <- Too late!  
fi 

Test your source'ed files as modular units before using them. Don't, ever, source a file you are not 100% confident in.

You say that you don't run them in background because you need to share the variables: take a look at your design again.

If you need to pass data then consider using a named pipe (a.k.a. a fifo, see man mkfifo) or some other form of interprocess communication.

Upvotes: 0

jmh
jmh

Reputation: 9336

What you are looking for is background shells and how to kill them.

The first step is to fork those scripts into the background instead of sourcing them. Sourcing them just inserts their code into your current script.

So you could do something like:

# '&' forks a script into the background
/path/script1.sh &
/path/script2.sh &
/path/script3.sh &
...
# now when you need to make sure that they are all killed
kill $(jobs -p)

Explanation of source

If I have 3 scripts:

script.1.sh:

echo "hello world"

script.2.sh:

source script.1.sh
source script.1.sh

script.3.sh:

echo "hello world"
echo "hello world"

There is no functional difference between script.3.sh and script.2.sh. None. They both will use one shell process (echo is builtin) and will call echo "hello world" twice one after the other.

script4.sh:

script.1.sh &
script.1.sh &

This one is different. It forks two background scripts (both called script.1.sh although they will have different process IDs), each of those background processes will echo "hello world" and then exit. It will look the same, but in this case you could actually kill one of them since they are executing asynchronously from your main script.

Upvotes: 1

Chirlo
Chirlo

Reputation: 6132

A script is not a process, it's only interpreted by the shell (it might spawn other process though). Since it's not a process, it doesn't get assigned a pid, which means you couldn't kill it. What you can kill is process started by those other scripts. For example, if script1.sh starts a wget download, you could then kill that wget process ( you'd need its pid in any case, to make sure that's the one you need to kill)

Upvotes: 0

Related Questions