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?
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:
Explanation of
sourceIf I have 3 scripts:
script.1.sh:
script.2.sh:
script.3.sh:
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:
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.