Run Bash Script from Another Script Without Waiting for Script to Finish Executing

Run bash script from another script without waiting for script to finish executing?

Put & at the end of the line.

./script1.sh & #this doesn't blocks!
./script2.sh

How to loop run an executable multiple time without waiting for it to finish

Bash executes foreground processes in a synchronous way. To do it as you want, you have to launch the process in the background using &, as pointed out in the comments. You can make a function that implements the desired behavior for a file and then call it inside the loop.

The only problem with a separate asynchronous function, is that the counter variable will not necessarily match your actual numbered filenames, so if you want them to keep the original number, you must extract it explicitly from the original name, or use an explicit variable. I will use the second approach in the following snippet:

#!/bin/bash

# Function to encapsulate your processing
# First arg: input file
# Second arg: counter
process_file(){
cd /some/dir/for/out/files/outputs/
mkdir file$2
cd file$2
/dir/where/executable/located/./run $1
}

for i in {0..65..1} # Iterate through the numbers, not the files, so we can use $i as a counter
do
input_file=/directory/where/input/files/are/file$i.txt
process_file $input_file $i & # Ampersand to launch in the background
done

Edit:
This is actually overkill for this case, as I'm pretty sure having you very same script, but with & appended to your run command, would do it. However, this approach is scalable in case you want to add more sequential processing for each single file and still have it in parallel for all files.

Bash Script Calls another bash script and waits for it to complete before proceeding

Normally it does; something else is happening. Are you sure that the other script isn't running something in the background instead? You can try using wait regardless.

Parallelise a Shell script without waiting for a batch to finish

The desired queue behavior, (but not necessarily the CPU assigning), can be made to work with command grouping like so:

{ ./hello<params1.txt && ./hello<params3.txt ; } &
{ ./hello<params2.txt && ./hello<params4.txt ; }

Demo of something like the above:

{ { echo a && sleep 2 && echo b ; } & 
{ echo c && sleep 1 && echo d ; } } | tr '\n' ' ' ; echo

Output:

a c d b 

execute bash script from php without waiting

This has to work:

exec('/your/command /dev/null 2>/dev/null &');

Wait for .sh script to finish before executing another .sh script?

Simply use the && connector (i.e. a compound-command):

./script1.sh && ./script2.sh

But please consider that in this case the second script will be executed only if the first returns a 0 exit code (i.e. no error). If you want to execute the sequence whatever the result of the first script, simply go:

./script1.sh ; ./script2.sh

You can test the behaviour of the two connectors:

$ true && echo aa
aa
$ false && echo aa
$ false ; echo aa
aa


Related Topics



Leave a reply



Submit