1

I am trying to write a bash script that runs a process and parses its output line by line (like here).

I would also like to get the process PID so for each line I can run ps and get CPU and memory usage (and print them with the output line).

I know I can get the PID with $! if I run the process in background, but then I won't know how to read the output.

Thanks in advance

Elad Weiss
  • 3,662
  • 3
  • 22
  • 50
  • 1
    The process has to be started in the background if you want the lines processed in real time, otherwise I believe you have to wait until the process exits. If that is what you want, check out https://stackoverflow.com/questions/20017805/bash-capture-output-of-command-run-in-background – Calvin Taylor Nov 13 '17 at 09:22

1 Answers1

3

You can create a FIFO and read that while the background process is running. For each line you read, you can do whatever you want with the child_pid.

First we need a small sample program:

bgp() { sleep 1; echo 1; sleep 1; echo 2; sleep 1; echo 3; }

Then create a fifo (maybe choose some path in /tmp)

tmp_fifo="/tmp/my_fifo"
rm "${tmp_fifo}" &>/dev/null
mkfifo "${tmp_fifo}"

Start your process in the background and redirect the output to the FIFO:

bgp > "${tmp_fifo}" &
child_pid=$!

Now read the output until the child process dies.

while true; do
    if jobs %% >&/dev/null; then
        if read -r -u 9 line; then
            # Do whatever with $child_pid
            echo -n "output from [$child_pid]: "
            echo $line
        fi
    else
        echo "info: child finished or died"
        break
    fi
done 9< "${tmp_fifo}"
nyronium
  • 1,258
  • 2
  • 11
  • 25