39

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"

migrated from serverfault.com May 22 '13 at 16:53

This question came from our site for system and network administrators.

65

The key is the "wait" command:

#!/bin/bash

/my/process &
/another/process &
wait
echo "All processes done!"
  • 7
    Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;) – Pascal Schmiel May 22 '13 at 12:47
  • 2
    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 '15 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 $!; – William Pursell Aug 2 '17 at 17:26
29

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
  • 2
    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 '15 at 14:48
  • @cuonglm, What is there are multiple processes? Probably wait is more suitable for that? – Nishant Dec 5 '18 at 9:30
  • @cuonglm what does -0 option to kill command do – k_vishwanath Aug 26 at 8:49
  • 1
    @k_vishwanath it report whether process is still running or not. – cuonglm Aug 26 at 16:22
29

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.'
0

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

  nohup command &
  • 3
    (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 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
  • (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 Sep 16 '17 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.