Kiran
Kiran

Reputation: 8548

Running the shell scripts in Background

I would to give the user the feature to run the shell script in background.

My shell program instantiates a number of other shell scripts.

Here is a small code snippet of my script

./main.sh  # Main script

in main.sh 

I call preprocessing.sh
create_dir.sh
handle_file.sh
post_processing.sh
report_generation.sh

I would like to know if I have to initiate all the child script as well.. What is the syntax if i have to initiate all the scripts in background and at the end inform the user by displaying message in that test run is complete.

Thanks

Kiran

Upvotes: 2

Views: 3058

Answers (2)

Martin Wickman
Martin Wickman

Reputation: 19925

Start your processes in the background with & and then use bash's builtin wait command:

  wait [n ...] 
          Wait for each specified process and return its termination  sta‐ 
          tus.   Each  n  may be a process ID or a job specification; if a 
          job spec is given, all processes  in  that  job’s  pipeline  are 
          waited  for.  

A couple of example are available here. For instance:

# wait on 2 processes
sleep 10 &
sleep 10 &
wait %1 %2 && echo "Completed!"

Upvotes: 1

David Fox
David Fox

Reputation: 10753

add "&" to the end of the commands

I call preprocessing.sh &
create_dir.sh &
...

Upvotes: 0

Related Questions