46

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"
77

The key is the "wait" command:

#!/bin/bash

/my/process &
/another/process &
wait
echo "All processes done!"
| improve this answer | |
  • 8
    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
37

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.'
| improve this answer | |
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
| improve this answer | |
  • 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 '19 at 8:49
  • 1
    @k_vishwanath it report whether process is still running or not. – cuonglm Aug 26 '19 at 16:22
0

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

  nohup command &
| improve this answer | |
  • 5
    (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
  • 1
    (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 1:38
-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
| improve this answer | |

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.