63

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"
0wn3r
  • 731

5 Answers5

101

The key is the "wait" command:

#!/bin/bash

/my/process &
/another/process &
wait
echo "All processes done!"
52

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.'
Mircea Vutcovici
  • 1,834
  • 14
  • 9
46

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
cuonglm
  • 153,898
1

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 echo "PROCESS IS RUNNING" sleep 1 done

0

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? – G-Man Says 'Reinstate Monica' Oct 23 '15 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. – Alexej Magura Sep 15 '17 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? – G-Man Says 'Reinstate Monica' Sep 16 '17 at 01:38