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


5 Answers 5


The key is the "wait" command:


/my/process &
/another/process &
echo "All processes done!"
  • 10
    Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;) May 22, 2013 at 12:47
  • 3
    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.
    – Mark Booth
    Apr 28, 2015 at 10:30
  • 3
    @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 $!; Aug 2, 2017 at 17:26

With wait you can have the granularity you need:

sleep 1 &
sleep 2 &

wait $PID1
echo 'PID1 has ended.'
echo 'All background processes have exited.'

Here is one way to do it:

launch backgroundprocess &

while kill -0 "$PROC_ID" >/dev/null 2>&1; do
exit 0
  • 6
    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)
    – Mark Booth
    Apr 30, 2015 at 14:48
  • @cuonglm, What is there are multiple processes? Probably wait is more suitable for that?
    – Nishant
    Dec 5, 2018 at 9:30
  • @cuonglm what does -0 option to kill command do Aug 26, 2019 at 8:49
  • 1
    @k_vishwanath it report whether process is still running or not.
    – cuonglm
    Aug 26, 2019 at 16:22
  • how would you capture/know the exit status of the process when it terminates ?
    – Pieter
    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=<process ID>
START_TIME=$(cut -d ' ' -f 22 /proc/$PID/stat)

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

You can run your process with nohup and write shell script to read nohup.out file which nohup uses to log .

  nohup command &
  • 6
    (1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output? Oct 23, 2015 at 13:29
  • @G-Man maybe by checking the output of lsof to see if nohup is still using nohup.out, but I agree this is a very hairy method. Sep 15, 2017 at 16:39
  • 2
    (4) By the time command starts running, nohup is gone. (1) Yes, I’m repeating a number, because I’m repeating what I said two years ago: nohup doesn’t write anything to nohup.out. (5) Yes, you could write a shell script to loop and run lsof to see whether nohup.out is still open. But that would be a different answer. (6) Even if you did that, it would be unreliable. What if some other process opened the nohup.out file? You’d really want to check if this specific process had nohup.out open. (7) But, if you’re going to do that, why not just check whether the process is running? Sep 16, 2017 at 1:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .