Launch a background process and check when it ends

How can I launch a process in background and check when it ends within a bash script? My idea is a script like this:

launch backgroundprocess & while [ Process is running ];do echo "PROCESS IS RUNNING\r" done; echo "PROCESS TERMINATED" 
752 2 2 gold badges 9 9 silver badges 23 23 bronze badges asked May 22, 2013 at 12:34 741 1 1 gold badge 6 6 silver badges 5 5 bronze badges Commented Jan 11, 2022 at 16:19

5 Answers 5

The key is the "wait" command:

#!/bin/bash /my/process & /another/process & wait echo "All processes done!" 
answered May 22, 2013 at 12:39 Pascal Schmiel Pascal Schmiel 1,699 1 1 gold badge 11 11 silver badges 6 6 bronze badges Benefit of this: it does NOT burn CPU time with an infinite loop running all the time. ;) Commented May 22, 2013 at 12:47

The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this.

Commented Apr 28, 2015 at 10:30

@Mark Booth You can indicate progress with a third asynchronous job. eg: cmd1 & p=$!; cmd2 & q=$!; while sleep 1; do echo still running ; done& wait $p $q; kill $!;

Commented Aug 2, 2017 at 17:26

With wait you can have the granularity you need:

sleep 1 & PID1=$! sleep 2 & PID2=$! wait $PID1 echo 'PID1 has ended.' wait echo 'All background processes have exited.' 
answered May 22, 2013 at 13:57 Mircea Vutcovici Mircea Vutcovici 1,864 14 14 silver badges 9 9 bronze badges

Here is one way to do it:

launch backgroundprocess & PROC_ID=$! while kill -0 "$PROC_ID" >/dev/null 2>&1; do echo "PROCESS IS RUNNING" done echo "PROCESS TERMINATED" exit 0 
answered May 22, 2013 at 12:42 155k 39 39 gold badges 336 336 silver badges 413 413 bronze badges

Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question)

Commented Apr 30, 2015 at 14:48 @cuonglm, What is there are multiple processes? Probably wait is more suitable for that? Commented Dec 5, 2018 at 9:30 @cuonglm what does -0 option to kill command do Commented Aug 26, 2019 at 8:49 @k_vishwanath it report whether process is still running or not. Commented Aug 26, 2019 at 16:22 how would you capture/know the exit status of the process when it terminates ? Commented Sep 30, 2022 at 22:33

wait only works for a child process launched in the same shell; it's a shell function after all.

For any arbitrary background process you need to work with PID. What nobody here mentions is kernel PID recycling. Modern linux systems may reuse PID quite often depending on the load. You need more than just PID to reliably identify the process. One simple approach is to save the time when the process started and compare it regularly with the current process start time. Should PID be recycled its start time will not be the same.

Process start time is reported as field 22 in /proc/[pid]/stat file. The time is measured in jiffies (at most 10ms) which provides enough precision to detect process ID recycling.

PID= START_TIME=$(cut -d ' ' -f 22 /proc/$PID/stat) while [ "$(cut -d ' ' -f 22 /proc/$PID/stat 2>/dev/null)" = "$START_TIME" ]; do echo "PROCESS IS RUNNING" sleep 1 done