In Mac, how to do `tail -f --pid` to terminate after process exit like Linux? tail command in current Linux has --pid option to terminate after certain process id, while tail under MAC OSX doesn't have the same option.
e.g. tail -f --pid=&lt;process_pid&gt; can terminate tail mode if process_pid terminated.
So how can we get the same result as Linux tail -f --pid?
There are perhaps several possible ways you can accomplish that.
First solution (easiest): How to replace Mac OS X utilities with GNU core utilities?
Follow the above and install gnu tail. You can then possibly have a function or alias of tail to gtail. like
alias tail=gtail
or even a Bash function:
tail(){
  command gtail &quot;$@&quot;
}

or just simply call gtail altogether instead of BSD tail
Second solution: The second way of doing that would be to make your own function, which you can add in your bash_profile or make a script, whatever suits you. In this script, all that we are doing is just to check if the PID exists or not. If it exists then we tail otherwise we exit with return code 1.
_tail(){
  # usage _tail -f &lt;file&gt; &lt;pid&gt;
  function _tail::trap() {
    trap - SIGTERM &amp;&amp; kill -- -$$
  }
  local FILE=&quot;$1&quot;
  local PID=&quot;$2&quot;
  if ps -p &quot;$PID&quot; &gt; /dev/null; then
    trap _tail::trap SIGINT
    command tail -f &quot;${FILE}&quot; &amp;
    local pid=$!
    disown # Optional: if you want to supress the kill SIGINT message
    local run=true
    while ${run}; do
      if ! $(ps -p ${PID} &gt; /dev/null); then
        kill -9 ${pid} &gt; /dev/null 2&gt;&amp;1
        run=false
      fi
    done
  else
    return 1
  fi
}

Now, you can call this function like _tail &lt;file-to-tail&gt; &lt;pid&gt;, which will do exactly same thing as gtail -f &lt;file-to-tail&gt; --pid=&lt;pid&gt;
However, if you want, you can extend it further and make it same syntaxt as gnu tail by adding another function called tail and sourcing it in .bash_profile or in your path as executable.
tail(){
  # usage: tail -f &lt;file&gt; --pid=&lt;pid&gt;
  local fFlag=&quot;$1&quot;
  local FILE=&quot;$2&quot;
  local PIDARGS=&quot;$3&quot;
  local PIDARG=&quot;$(echo ${PIDARGS} | cut -d '=' -f1)&quot;
  local PID=&quot;$(echo ${PIDARGS} | cut -d '=' -f2)&quot;
  if [[ &quot;$fFlag&quot; == &quot;-f&quot; ]] &amp;&amp; [[ -f &quot;${FILE}&quot; ]] &amp;&amp; \
    [[ &quot;${PIDARG}&quot; == &quot;--pid&quot; ]] &amp;&amp; [[ $(ps -p &quot;${PID}&quot;) ]]; then
    _tail &quot;${FILE}&quot; &quot;${PID}&quot;
  else
    command tail &quot;$@&quot;
  fi
}

[[ &quot;${BASH_SOURCE[0]}&quot; == &quot;$0&quot; ]] &amp;&amp; tail &quot;$@&quot;