You'll want to use the wait command to do this for you. You can either capture all of the children process IDs and wait for them specifically, or if they are the only background processes your script is creating, you can just call wait without an argument. For example:

#!/bin/bash
# run two processes in the background and wait for them to finish

nohup sleep 3 &
nohup sleep 10 &

echo "This will wait until both are done"
date
wait
date
echo "Done"
Answer from ParanoidGeek on Stack Exchange
Top answer
1 of 3
91

Shell scripts, no matter how they are executed, execute one command after the other. So your code will execute results.sh after the last command of st_new.sh has finished.

Now there is a special command which messes this up: &

cmd &

means: "Start a new background process and execute cmd in it. After starting the background process, immediately continue with the next command in the script."

That means & doesn't wait for cmd to do it's work. My guess is that st_new.sh contains such a command. If that is the case, then you need to modify the script:

cmd &
BACK_PID=$!

This puts the process ID (PID) of the new background process in the variable BACK_PID. You can then wait for it to end:

while kill -0 $BACK_PID ; do
    echo "Process is still active..."
    sleep 1
    # You can add a timeout here if you want
done

or, if you don't want any special handling/output simply

wait $BACK_PID

Note that some programs automatically start a background process when you run them, even if you omit the &. Check the documentation, they often have an option to write their PID to a file or you can run them in the foreground with an option and then use the shell's & command instead to get the PID.

2 of 3
6

Make sure that st_new.sh does something at the end what you can recognize (like touch /tmp/st_new.tmp when you remove the file first and always start one instance of st_new.sh).
Then make a polling loop. First sleep the normal time you think you should wait, and wait short time in every loop. This will result in something like

max_retry=20
retry=0
sleep 10 # Minimum time for st_new.sh to finish
while [ ${retry} -lt ${max_retry} ]; do
   if [ -f /tmp/st_new.tmp ]; then
      break # call results.sh outside loop
   else
      (( retry = retry + 1 ))
      sleep 1
   fi
done
if [ -f /tmp/st_new.tmp ]; then
   source ../../results.sh 
   rm -f /tmp/st_new.tmp
else
   echo Something wrong with st_new.sh
fi
Discussions

tar - Wait for process to finish before going to the next line in shell script - Unix & Linux Stack Exchange
I have a script I made to create a backup. I need to make sure the backup is ready before it runs the /home/ftp.sh command. How can I do so? I use CentOS 5.6 #!/bin/bash tar -Pcf /home/temp_backup... More on unix.stackexchange.com
🌐 unix.stackexchange.com
bash - Is there a command to wait X seconds before next command? - Unix & Linux Stack Exchange
Say I want to execute two commands but I want to wait X amount of seconds before the next one gets executed, what's the command for this? E.g. sudo dnf upgrade -y && [PAUSE X SECONDS] &... More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 30, 2021
Getting a bash script to wait for wget to complete before beginning the next step
resume the script once wget has completed That's the default behavior for a BASH script. Each command must complete before the next one gets run. Presumably the wget is failing for some reason. More on reddit.com
🌐 r/linuxquestions
40
12
April 13, 2018
Is it possible for bash commands to continue before the result of the previous command? - Stack Overflow
When running commands from a bash script, does bash always wait for the previous command to complete, or does it just start the command then go on to the next one? ie: If you run the following two More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is the function of the Bash Wait Command?
The wait command halts scriptexecution until background processes finish.
🌐
cyberpanel.net
cyberpanel.net › blog › bash-wait-command
Mastering Bash Wait Command: A Complete Guide
Does the bash wait command give back status of exit?
Yes, it gives back the exit status of the last process it waited on.
🌐
cyberpanel.net
cyberpanel.net › blog › bash-wait-command
Mastering Bash Wait Command: A Complete Guide
How do I wait for multiple commands in bash?
To wait on specific processes, use "wait PID1 PID2 ...".
🌐
cyberpanel.net
cyberpanel.net › blog › bash-wait-command
Mastering Bash Wait Command: A Complete Guide
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › bash-how-to-wait-in-script-from-the-previous-command-to-finish-4175602032
[SOLVED] bash how to wait in script from the previous command to finish
March 17, 2017 - Hi everyone Ayone here have an idea how to hang a bash script until previous command is finished ? I an opening a gnome-terminal from a bash script
🌐
CyberPanel
cyberpanel.net › blog › bash-wait-command
Mastering Bash Wait Command: A Complete Guide
February 2, 2026 - This ensures that myscript. sh) runs in the background and the system waits for it to complete before moving on. ... The wait command halts script execution until background processes finish.
🌐
DiskInternals
diskinternals.com › home › linux reader › whether bash waits for command to finish
Whether bash waits for command to finish | DiskInternals
July 29, 2021 - The bash WAIT command is used to halt the execution of a script until all background jobs or specified JobID/PIDs terminate successfully and return an expected exit code to trigger the next command that was “waited for.” · Simply, the bash ...
🌐
Quora
quora.com › Does-Bash-script-wait-for-one-process-to-finish-before-executing-another
Does Bash script wait for one process to finish before executing another? - Quora
Answer (1 of 3): bash and other Bourne compatible shells have very simple (and somewhat limited) job management features. As others in this thread have pointed out you generally run jobs in the background by appending the & operator to the end of a command. At that point the PID (process ID) of ...
🌐
Namehero
namehero.com › blog › timing-matters-getting-to-know-the-bash-wait-command
Timing Matters: Getting To Know The Bash Wait Command
November 6, 2024 - The bash wait command is a built-in command that allows scriptwriters to pause the execution of a script until all background processes are completed. This ensures that subsequent commands are executed only after the specified processes have ...
Find elsewhere
🌐
Linux Hint
linuxhint.com › wait-command-linux
How to Wait for a Specific Process to Complete in Linux? – Linux Hint
The next command of wait does not use any flag which means that this wait command will wait for all the processes to be completed first and then it displays the echo statement “Completed all job”. #!/bin/bash sleep 10 & sleep 5 & sleep 3 & wait -n echo “Completed first job.” wait -n ...
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › bash wait command with examples
Bash wait Command with Examples
December 11, 2025 - Use the wait command to indicate by what point a background process must execute inside a script. 1. For example, add the following code in a text editor: #!/bin/bash echo Background process & echo First message echo Second message wait echo Third message · If the background process does not finish the first and second processes, the wait command invokes a pause to wait for the background process to complete after the second process before continuing to the third process.
🌐
TutorialsPoint
tutorialspoint.com › bash-wait-command-with-examples
Bash wait Command with Examples
April 12, 2023 - We can handle this error using '||' operator, which will execute command on right-hand side of operator only if command on left-hand side returns a non-zero exit code. The wait command can also be used with pipelines.
🌐
Baeldung
baeldung.com › home › scripting › start script after another finishes in linux
Start Script After Another Finishes in Linux | Baeldung on Linux
March 18, 2024 - We can launch a script in the background initially and later wait for it to finish before executing another script using the wait command. We use the single & operator to launch background processes: $ ./script1.sh & $ Sleeping for 60 seconds...
🌐
Bashscript
bashscript.net › how-to-make-a-bash-script-wait-for-a-command-to-finish
How to Make a Bash Script Wait for a Command to Finish – BashScript.net
December 20, 2024 - Below is a simple Bash script that demonstrates how to initiate a background process, captures its PID, and waits for its completion before executing further commands. In this example, we simulate a long-running task using the sleep command. #!/bin/bash # Start a background process date # Print current time sleep 5 & # Capture the Process ID PID=$! # Wait for the background process to finish echo "Waiting for the background process with $PID ID to finish..." wait $PID # Continue with the script date # Print current time after command completion echo "Background process has finished."
🌐
Baeldung
baeldung.com › home › scripting › the wait command in linux
The wait Command in Linux | Baeldung on Linux
March 18, 2024 - ... echo parent process $$ starting child & sleep 1 child & wait echo parent wait finished, exit status $? Here, the parent process will perform these steps: echo a starting message that contains its process ID · create two background processes ...
🌐
Linuxize
linuxize.com › home › bash › bash wait command
Bash wait Command | Linuxize
January 30, 2026 - The -f option tells wait to wait for each PID or jobspec to actually terminate before returning its exit code, rather than returning when the job status changes (e.g., when stopped with kill -STOP).
🌐
Reddit
reddit.com › r/linuxquestions › getting a bash script to wait for wget to complete before beginning the next step
r/linuxquestions on Reddit: Getting a bash script to wait for wget to complete before beginning the next step
April 13, 2018 -

Hi guys,

I’ve just started learning to how to write bash scripts and one of my scripts basically depends on downloading a file using wget, then executing the file. The problem is as it stands the script just runs through each command and nothing gets downloaded with wget.

What i’d like it to do is download the file, show its progress and then resume the script once wget has completed. Is this at all possible?

Pastebin of my script: https://pastebin.com/11M1TWfr

EDIT: Before anyone else points it out - the serial number there is not a purchased one. I don't care if you use it, I found it off a website myself.

EDIT 2: Got it working now, not entirely sure how but I basically added this script into a different script I've written and it actually seems to give output now...I must have done something wrong in the original script! Thank you so much to everyone who took the time to answer this, I really appreciate the help. Let it never be said that the Linux community is unhelpful :)!