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.

Answer from Aaron Digulla on Stack Overflow
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

Wait for one processes to complete in a shell script
Let's say I start process A.sh, then start process B.sh. I call both of them in my C.sh How can I make sure that B starts its execution only after A.sh finishes. I have to do this in loop.Execution time of A.sh may vary everytime. It is a parameterized script. More on community.unix.com
๐ŸŒ community.unix.com
0
0
June 19, 2013
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
Waiting for any process to finish in bash script - Unix & Linux Stack Exchange
I am trying to check how many active processes are running in a bash script. The idea is to keep x processes running and when one finished then the next process is started. For testing purposes I s... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
June 28, 2021
process - How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0? - Stack Overflow
How to wait in a bash script for several subprocesses spawned from that script to finish, and then return exit code !=0 when any of the subprocesses ends with code !=0? Simple script: #!/bin/bash f... More on stackoverflow.com
๐ŸŒ stackoverflow.com
People also ask

What is theโ€‚function of the Bash Wait Command?
The wait command halts scriptโ€‚execution 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
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ sysadmin โ€บ bash wait command with examples
Bash wait Command with Examples
December 11, 2025 - The bash wait is a shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.
๐ŸŒ
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
๐ŸŒ
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).
๐ŸŒ
Unix Community
community.unix.com โ€บ shell programming and scripting
Wait for one processes to complete in a shell script - Shell Programming and Scripting - Unix Linux Community
June 19, 2013 - Let's say I start process A.sh, then start process B.sh. I call both of them in my C.sh How can I make sure that B starts its execution only after A.sh finishes. I have to do this in loop.Execution time of A.sh may vary everytime. It is a parameterized script.
Find elsewhere
๐ŸŒ
CyberPanel
cyberpanel.net โ€บ blog โ€บ bash-wait-command
Mastering Bash Wait Command: A Complete Guide
February 2, 2026 - The Bash wait command is a built-in utility that pauses script execution until specified background processes are complete. It also decides when to go ahead with the process execution.
๐ŸŒ
DiskInternals
diskinternals.com โ€บ home โ€บ linux reader โ€บ whether bash waits for command to finish
Whether bash waits for command to finish | DiskInternals
July 29, 2021 - Simply, the bash WAIT command is used to prevent a script from exiting before a background process or job is completed.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ wait-command-linux
How to Wait for a Specific Process to Complete in Linux? โ€“ Linux Hint
The wait with the option -n commands pause for 3 and 5 seconds which is the amount of time it takes for the first and next job to finish the wait commands then pause for ten seconds which is the amount of time it takes for the remaining background processes to finish.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ linux-unix โ€บ shell-script-to-demonstrate-wait-command-in-linux
Shell Script to Demonstrate Wait Command in Linux - GeeksforGeeks
June 2, 2022 - Display that we are running multiple processes. Using wait -f to wait until all the above process has been executed successfully. After the process is finished we are printing its exit status using a special variable $?.
Top answer
1 of 3
3

While wait -n (per comment @icarus) works in this particular situation, it should be noted that$! contains the PID of the last process that is started. So you could test on that as well:

#!/bin/bash

find $HOME/Downloads -name "dummy" &
p1=$!
find $HOME/Downloads -name "dummy" &
p2=$!
find $HOME/Downloads -name "dummy" &
p3=$!
while true
do
    if ps $p1 > /dev/null ; then
        echo -n "p1 runs "
    else 
        echo -n "p1 ended"
    fi
    if ps $p2 > /dev/null ; then
        echo -n "p2 runs "
    else 
        echo -n "p2 ended"
    fi
    if ps $p1 > /dev/null ; then
        echo -n "p3 runs "
    else 
        echo -n "p3 ended"
    fi
    echo ''
    sleep 1
done

But parallel is a better option.

2 of 3
2

The problem with the script is that there is nothing in it which is going to call one of the wait system calls. Generally until something calls wait the kernel is going to keep an entry for the process as this is where the return code of the child process is stored. If a parent process ends before a child process the child process is reparented, usually to PID 1. Once the system is booted, PID 1 often is programmed to enter a loop just calling wait to collect these processes exit value.

Rewriting the test script to call the shell builtin function wait we get

pids=()
find $HOME/Downloads -name "dummy" &
pids+=( $! )
find $HOME/Downloads -name "dummy" &
pids+=( $! )
find $HOME/Downloads -name "dummy" &
pids+=( $! )

echo "Initial active processes: ${#pids[@]}"
for ((i=${#pids[@]}; i>1; i--)) ; do 
do
    wait -n # Wait for  one process to exit
    echo "A process exited with RC=$?"
    # Note that -n is a bash extension, not in POSIX
    # if we have bash 5.1 then we can use "wait -np EX" to find which
    # job has finished, the value is put in $EX. Then we can remove the
    # value from the pids array. 

    echo "Still outstanding $(jobs -p)"
    sleep 1
done
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ scripting โ€บ the wait command in linux
The wait Command in Linux | Baeldung on Linux
March 18, 2024 - The wait command does exactly this ... explore how we can use the wait command. The wait command makes a shell script or terminal session wait for background processes to finish....
๐ŸŒ
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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ bash-wait-command-with-examples
Bash wait Command with Examples
April 12, 2023 - We can also use wait command with trap command to handle signals and terminate background processes when necessary. For example, suppose we have a script that starts a background process and we want to terminate it if user presses Ctrl+C ? #!/bin/bash trap "kill %1" SIGINT sleep 20 & echo "Command running in background..." wait echo "Command has completed!"...
๐ŸŒ
Quora
quora.com โ€บ What-are-some-good-ways-to-make-a-bash-script-wait-until-another-instance-of-the-script-has-completed-running
What are some good ways to make a bash script wait until another instance of the script has completed running? - Quora
What are some good ways to make a bash script wait until another instance of the script has completed running? In two words: use flock. Itโ€™s a standard command-line utility on every Linux distribution Iโ€™ve seen, and for your scenario, it sounds like itโ€™s as easy as invoking each instance of your script like this:
Top answer
1 of 1
2

No. The screen -X stuff command will return immediately, because it is not aware that it is being asked to run a command in the first place, even more so when that command is complete โ€“ all it's doing is injecting keypresses (which is why you had to add the ^M manually). As soon as the fake tty input has been sent, the command returns and your script continues.

In general, as a terminal emulator, screen does not know what exactly is happening in any specific terminal window โ€“ there's nothing that would delimit a shell prompt (or any other interactive console prompt) from regular output.

(This is not fundamentally impossible โ€“ some terminal emulators allow the shell to output "output start/end" and "prompt start/end" markers, e.g. VSCode will inject special configuration into Bash to achieve exactly that โ€“ but it requires support from the terminal emulator and cooperation from every program that would show an input prompt, and neither Screen nor Minecraft will do that currently.)

On the other hand, your script might have an easier time because it only deals with a single program, which means it only needs to wait for a specific prompt to appear, not just any prompt in general. You could implement this by having a loop that would query Screen for the current buffer "contents" using -X hardcopy, and if the last line of that buffer is not the Minecraft prompt yet, sleep 1 second and repeat.

This is similar to how the expect program works: one can write an Expect script to automate various kinds of interactive input, but the core of such a script is always a set of expect "this" and expect "that", i.e. knowing up-front that some specific text is a "prompt" and waiting for that text.

[...]
expect "Password:" { send "$password\r" }
expect ">" { send "enable\r" }
expect "Password:" { send "$enablepwd\r" }
expect "#" { send "show run\r" }

What other Minecraft-management scripts do (such as this project, which recently switched from Screen to tmux) appears to be 1) blindly submit both "save-all" and "stop" at once, 2) wait for the server to process bot commands until it exits by itself. That is, instead of waiting for the command to complete, they wait for the result of that command.

Similarly, if you want to wait for the save to finish, you don't need to wait for the "save-all" command โ€“ you can instead use inotifywait to wait until the server has finished writing a specific file. This can be somewhat tricky to do right (normally you'd need to use "coproc" to start inotifywait before issuing the command so that you wouldn't miss the event), but as a save is likely to take a while, it's probably fine to just do it after.

echo "waiting..."
inotifywait -q -e close_write /path/to/game
echo "probably done!"

Some programs deliberately create a specific file last, so that other tools could wait for it to appear:

echo "waiting..."
until [ -e /path/to/marker_file ]; do sleep 1; done
echo "marker file showed up"
๐ŸŒ
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 ...